I'm trying to read a CSV file from an FTP server and parse it on app engine.
I can access the file and read it to StringIO
but when I try to loop over the files lines it just loops over every character instead of lines. Not sure what I'm doing wrong here:
ftp = FTP('ftp.mydomain.com', 'username', 'pwd')
ftp.set_pasv(True)
r = StringIO()
ftp.retrbinary('RETR test.csv', r.write)
csvfile = csv.reader(r.getvalue(), delimiter=',')
for line in csvfile:
print line
this ends up in something like this:
['O']
['R']
['D']
['E']
['R']
['N']
['O']
['', '']
['O']
['R']
['D']
['E']
['R']
['D']
['A']
['T']
['E']
['', '']
['I']
['N']
['V']
['O']
['I']
['C']
['E']
['N']
['O']
['', '']
...
What is the correct way to do this and correctly parse the file from FTP so the csv
module can read it correctly?
Split the long string on newlines; csv.reader()
expects an iterable, where each iteration a line is yielded. You are giving it a string, iteration is over individual characters then:
csvfile = csv.reader(r.getvalue().splitlines(), delimiter=',')
You don't show how StringIO()
was imported. If it is the python version (from StringIO import StringIO
) you can simply seek back to the start and pass that in directly:
r.seek(0)
csvfile = csv.reader(r, delimiter=',')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With