Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 973
  • Last Modified:

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
0
DannyJOsborne
Asked:
DannyJOsborne
  • 5
  • 5
1 Solution
 
clockwatcherCommented:
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
 
DannyJOsborneAuthor Commented:
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
 
clockwatcherCommented:
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
[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

 
DannyJOsborneAuthor Commented:
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
 
clockwatcherCommented:
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
 
clockwatcherCommented:
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
 
DannyJOsborneAuthor Commented:
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
 
clockwatcherCommented:
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
 
DannyJOsborneAuthor Commented:
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
 
DannyJOsborneAuthor Commented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now