Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with stripping output
#1
Using Linux Mint 18.2 and Python 2.7.12.
The routine runs fine the way it is. However the output is too busy with
characters " [] ' , ". I have done many searches looking for the correct
way to strip the characters without success. I wish to print the lines
in my program.
Please help.
tia
oldcity




 #!/usr/bin/python
 #
 line_num = 0
 dcats1 = []
 dcats2 = []
 with open('CATAGORIES.DAT', 'r') as catgs:
     for line in catgs:
         dfiles = line.strip(",")
         dfiles = dfiles.rstrip("\n")
         line_num+=1
         if (line_num < 6):
            dcats1.append(dfiles,)
            dcatagory1 = dcats1
         if (line_num > 5):
            dcats2.append(dfiles,)
            dcatagory2 = dcats2



 print dcatagory1
 print ("\n\n")
 print dcatagory2
 print ("\n\n")
 print 'job done'
 oldcity ~/MyPython $ python new-test-it.py
 [' 1. AT&T  ', ' 2. FP&L  ', ' 3. WATER ', ' 4. GAS   ', ' 5. PLAN-D']



 [' 6. AARP  ', ' 7. DIAZ  ', ' 8. DRUGS ', ' 9. HOUSE ', '10. CAR   ', '11. MISC  ']
Reply
#2
>>> import itertools
>>> 
>>> dcatagory1 = [' 1. AT&T  ', ' 2. FP&L  ', ' 3. WATER ', ' 4. GAS   ', ' 5. PLAN-D']
>>> dcatagory2 = [' 6. AARP  ', ' 7. DIAZ  ', ' 8. DRUGS ', ' 9. HOUSE ', '10. CAR   ', '11. MISC  ']
>>> for item in itertools.chain(dcatagory1, dcatagory2):
...     print(item.strip())
...     
1. AT&T
2. FP&L
3. WATER
4. GAS
5. PLAN-D
6. AARP
7. DIAZ
8. DRUGS
9. HOUSE
10. CAR
11. MISC
Look again at Linux Python 3 environment
So you can at least use Python 3.5 that comes with Mint 18.2.
Mint 19 has Python 3.6.5 as default.
Reply
#3
What I needed was a clean output as below.
For result from dcatagory1 and decatagory2
In other words display horizontal lines.

1. AT&T 2. FP&L 3. WATER 4. GAS 5. PLAN. D

6. AARP 7. DIAZ 8. DRUGS 9. HOUSE 10. CAR 11. MISC
Reply
#4
>>> lst = []
>>> dcatagory1 = [' 1. AT&T  ', ' 2. FP&L  ', ' 3. WATER ', ' 4. GAS   ', ' 5. PLAN-D']
>>> dcatagory2 = [' 6. AARP  ', ' 7. DIAZ  ', ' 8. DRUGS ', ' 9. HOUSE ', '10. CAR   ', '11. MISC  ']
>>> lst.append(dcatagory1)
>>> lst.append(dcatagory2)
>>> for item in lst:
...     print(*item) 
...     
 1. AT&T    2. FP&L    3. WATER   4. GAS     5. PLAN-D
 6. AARP    7. DIAZ    8. DRUGS   9. HOUSE  10. CAR    11. MISC
Reply
#5
Found that Python 3.5 is on my system
Did not get same results as you. Used with python and python3.
Used with print(*item) and with print(item). Results below.
Also do not see or understand how you stripped your output.

    As #!/usr/bin/python3

 oldcity ~/MyPython $ python Test-Prog.py
  File "Test-Prog.py", line 9
    print(*item) 
          ^
 SyntaxError: invalid syntax

  With print(item)
 oldcity ~/MyPython $ python Test-Prog.py

 [' 1. AT&T  ', ' 2. FP&L  ', ' 3. WATER ', ' 4. GAS   ', ' 5. PLAN-D']
 [' 6. AARP  ', ' 7. DIAZ  ', ' 8. DRUGS ', ' 9. HOUSE ', '10. CAR   ', '11. MISC  ']

    As  #!/usr/bin/python

 oldcity ~/MyPython $ python Test-Prog.py
  File "Test-Prog.py", line 9
    print(*item) 
          ^
 SyntaxError: invalid syntax

  With print(item)
 oldcity ~/MyPython $ python Test-Prog.py

 [' 1. AT&T  ', ' 2. FP&L  ', ' 3. WATER ', ' 4. GAS   ', ' 5. PLAN-D']
 [' 6. AARP  ', ' 7. DIAZ  ', ' 8. DRUGS ', ' 9. HOUSE ', '10. CAR   ', '11. MISC  ']
Reply
#6
(Oct-27-2018, 10:24 PM)oldcity Wrote: Found that Python 3.5 is on my system
Did not get same results as you. Used with python and python3.
Yes you been told that in several post and you still use python(this use python 2.7) you should use python3(will use python 3.5 on Mint 18.2).
So command is:
oldcity ~/MyPython $ python3 Test-Prog.py

print(*item) start unpack only work in Python 3.
Reply
#7
Solved.
Thank you.
I'm a little thick sometimes also may read too fast.
oldcity
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Stripping numbers from text ebolisa 4 1,441 Jul-18-2022, 06:59 PM
Last Post: deanhystad
  Looking for simple help - text file stripping DerekK 1 2,121 Mar-08-2019, 10:06 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020