Solved

python prettytable

Posted on 2014-12-18
10
511 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now