Solved

python prettytable

Posted on 2014-12-18
10
578 Views
Last Modified: 2014-12-21
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
0
Comment
Question by:DannyJOsborne
  • 5
  • 5
10 Comments
 
LVL 25

Expert Comment

by:clockwatcher
ID: 40508590
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

0
 

Author Comment

by:DannyJOsborne
ID: 40508595
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
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 40508600
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.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:DannyJOsborne
ID: 40508608
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:-)
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 40508610
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.
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 40508612
Do you want each of them in a separate table?  The above assumed you wanted them all combined into a single table.  Separate tables is much easier if that's what you're after:

from prettytable import from_csv
cf = open("output.txt", "r") 
for fname in cf:
    fname = fname.rstrip()
    if len(fname) == 0:  # have an empty line in the file list
        continue
    f = open(fname, "r")
    pt = from_csv(f)
    f.close()
    print(pt)
cf.close()

Open in new window

0
 

Author Comment

by:DannyJOsborne
ID: 40508699
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
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 40509380
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.
0
 

Author Comment

by:DannyJOsborne
ID: 40510537
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
0
 

Author Closing Comment

by:DannyJOsborne
ID: 40511518
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()
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question