Link to home
Start Free TrialLog in
Avatar of Danny Osborne
Danny Osborne

asked on

python prettytable

Hi

Total newbie outdoing myself before i've even started to learn python (time is the problem).

I have a text file which contains a list of csv files names. I want python to loop through each row from that text file, open the csv named in the row then pass the contents of the csv to prettytable.

Basically i'm trying to open up multiple csv's in prettytable.

Example of output.txt file contents -

ACCESS SHIPPING LTD.csv
ADANI SHIPPING (INDIA) PVT LTD.csv
ADELFIA SHIPPING ENTERPRISES.csv
AE NOMIKOS SHIPPING INVESTMENTS LTD.csv
AEGEAN SHIPPING MANAGEMENT SA.csv
AL SEER MARINE.csv
ALASSIA NEWSHIPS MANAGEMENT.csv
ALDABRA SOCIETA SAMPLICE.csv

script

import csv
from prettytable import from_csv
cf = open("output.txt", "r")
for rows in cf:
    pt = from_csv(rows)
print(pt)
fp.close()

I'e got this far but am getting "AttributeError: 'str' object has no attribute 'read'"

Danny
Avatar of clockwatcher
clockwatcher

Glancing at the prettytable docs, the from_csv function expects a file type object.  You're passing it a line from your file. In other words,

from prettytable import from_csv
cf = open("output.txt", "r") 
pt = from_csv(cf)
print(pt)
cf.close()

Open in new window

Avatar of Danny Osborne

ASKER

Thanks. but now I get the following when running the script:-S

Traceback (most recent call last):
  File "C:\Users\Danny\workspace\Learn-python\LearnPython\Open_csv.py", line 3, in <module>
    pt = from_csv(cf)
  File "C:\Python32\lib\site-packages\prettytable.py", line 1324, in from_csv
    table.add_row([x.strip() for x in row])
  File "C:\Python32\lib\site-packages\prettytable.py", line 802, in add_row
    raise Exception("Row has incorrect number of values, (actual) %d!=%d (expected)" %(len(row),len(self._field_names)))
Exception: Row has incorrect number of values, (actual) 3!=2 (expected)

Danny
Sounds like your CSV file doesnt have the same number of columns per row.  It's expecting 2 columns and got 3.  You might want to post your CSV.
Hi, right, got what you sent working just now. Your comment on rows prompted me to re-look only to notice I hadn't wrapped the values in the output.txt, which have white spaces, in hyphens , but that's not what i'm after - I don't want to display the output.txt in prettytables, but rather use the strings (rows) in output.txt to open up other csv files.

I need a way to loop through the contents of output.txt and pass each row as a file name to csv reader for opening the file in question, reading, then parsing it through prettytables.

This is why my initial code has a for loop in it, albeit the code doesn't work:-)

I have 380+ files to preview. I guess you can see why i want to use pretty tables to read them:-)
Sorry... I was on my phone and didn't get a real good look at your question.  Back on a computer now and I have a better idea of what you're trying to do.  Without looking into prettytable too much (which may actually have a way to just add rows), one way to do it would be something like this:

from prettytable import from_csv
import io
o = io.StringIO()
cf = open("output.txt", "r") 
skip_header = False
for fname in cf:
    fname = fname.rstrip()
    if len(fname) == 0:  # have an empty line in the file list
        continue
    f = open(fname, "r")
    first_line = True
    for line in f:
        if first_line and skip_header:
            first_line = False
            continue
        skip_header = True
        first_line = False
        o.write(line)
    f.close()
cf.close()
o.seek(0)
pt = from_csv(o)
print(pt)

Open in new window

The above assumes you're using python3, it would be slightly different under python2.
ASKER CERTIFIED SOLUTION
Avatar of clockwatcher
clockwatcher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, Thanks for your time on this. Looks like it's getting there. I'm still getting an error though

Traceback (most recent call last):
  File "C:\Users\Danny\workspace\Learn-python\LearnPython\Open_csv.py", line 10, in <module>
    f = open(fname, "r")
IOError: [Errno 2] No such file or directory: "'ACCESS SHIPPING LTD.csv'"

Problem seems to be that it's including the hyphens I used to prevent the white spaces from being read as column separators.

Danny
You need the names in your output.txt to match the names in  your filesystem.  If there are spaces in the names on the file system use spaces in your output.txt.
Ok great, I'll take a look on Monday. Again, thanks for your help. Have a good weekend and I'll get back to you.

Danny
Got it working! Thanks for the help. Looking good now.

One tiny extra - I want to save the prettytable output to a file. I've added a couple of lines but it's only writing the last few rows of the prettytable output to the file for some reason (console output is looking fine).

Looking at the prettytable wiki, I can't see anything wrong.

from prettytable import from_csv
cf = open("zzoutput.txt", "r")
for fname in cf:
    fname = fname.rstrip()
    fname = fname.replace("'", "")
    if len(fname) == 0:  # have an empty line in the file list
        continue        
    f = open("f/" + fname, "r")
    pt = from_csv(f)
    f.close()
    print(pt)    
    w = open('zzptout.txt','w')
    w.write(pt.get_string())
w.close()
cf.close()