Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

correctly parsing a CSV file from an FTP server with app engine

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?

like image 245
aschmid00 Avatar asked Jun 18 '14 17:06

aschmid00


1 Answers

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=',')
like image 54
Martijn Pieters Avatar answered Sep 23 '22 14:09

Martijn Pieters