Posts: 18
Threads: 8
Joined: Apr 2018
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 ']
Posts: 7,085
Threads: 122
Joined: Sep 2016
>>> 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.
Posts: 18
Threads: 8
Joined: Apr 2018
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
Posts: 7,085
Threads: 122
Joined: Sep 2016
>>> 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
Posts: 18
Threads: 8
Joined: Apr 2018
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 ']
Posts: 7,085
Threads: 122
Joined: Sep 2016
Oct-28-2018, 04:58 AM
(This post was last modified: Oct-28-2018, 04:58 AM by snippsat.)
(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.
Posts: 18
Threads: 8
Joined: Apr 2018
Solved.
Thank you.
I'm a little thick sometimes also may read too fast.
oldcity
|