nick
nick

Reputation: 149

Reading csv from FTP folder

I am trying to read csv file from FTP Folder

ftp = FTP('adr')  
ftp.login(user='xxxx', passwd = 'xxxxx')
r = StringIO()
ftp.retrbinary('RETR /DataLoadFolder/xxx/xxx/xxx/'+str(file_name),r.write)
r.seek(0)
csvfile1 = csv.reader(r,delimiter=';')
input_file = [list(line) for line in csv.reader(r)] ----- Error

getting an error at last line as

new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

My csv file

enter image description here

Text Version
enter image description here

There are whites spaces at the end of each row (after 17.00)

Data starts from second row

what does the error mean? Any help would be much appreciated.

Upvotes: 0

Views: 1583

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 148880

I could partially reproduce and fix. The error is caused by a line containing a bad end of line. I could reproduce by adding a line \r \n at the end of an otherway valid csv file.

A simple way to fix it is to use a filter to eliminate blank lines and clean end of lines:

def filter_bytes(fd):
    for line in fd:
        line = line.strip()
        if len(line) != 0:
            yield(line + b'\r\n')

Once this is done, your code could become:

ftp = FTP('adr')  
ftp.login(user='xxxx', passwd = 'xxxxx')
r = BytesIO()
ftp.retrbinary('RETR /DataLoadFolder/xxx/xxx/xxx/'+str(file_name),r.write)
r.seek(0)
csvfile1 = csv.reader(filter_bytes(r),delimiter=';')
input_file = list(csvfile1)

Upvotes: 1

hcheung
hcheung

Reputation: 4014

The error message simply asking how you'd want to handle the newline differently due to historical reasons, you can read the explanation here.

To solve the issue, specify the newline on StringIO like this:

r = StringIO(newline='')

According to StringIO documentation. If newline is set to None, newlines are written as \n on all platforms, but universal newline decoding is still performed when reading.

Upvotes: 1

Related Questions