0

I am trying to write a file like object in memory. I downloaded a file on ftp and when I try to convert it to a StringIO or BytesIO object using the method read I am getting this:

"Android 4.2.2 Google Play 4 GB Bluetooth Dubbele sim & dubbele stand-by Simlockvrij Scherm: 4.0"" OLED nHD multi-touch Processor: Dual-Core Cortex A7 1,3 GHz Mobiele data/3G: HSDPA 7.2 Mbps / HSUPA";90,720000;3,00000000;,00000000;1,00000000;;ANDROID e ACCESSORI;SMARTPHONE e LED e PRODOTTI COOL;ACCESSORI TABLET e SMARTPHONE;,1100000000;,2000000000;,1000000000;1,0000000000;https://x-yasmp40200_u.jpg;https://x2/00000000000000005268-art-icol-yasmp40200_u.jpg;888888888;2,00000000\r\n'

This is the code:

with closing(request.urlopen("ftp://{}:{}@ftp.url/file.csv".format(USERNAME, PASSWORD))) as r:
    b = io.BytesIO(r.read())
    print(b.read())

Why are the \r\n chars converted to a string and is the ' char escaped? How to properly get the file content?

1 Answer 1

0

I discovered that the right encoding format is iso-8859-1:

     b = io.BytesIO()
     b.write(r.read())
     b.seek(0)
     lines = [line.decode("iso-8859-1") for line in b.readlines()]
     csvr = csv.reader(lines, delimiter=';')
     for row in csvr:
        print(row)
        break

Not the answer you're looking for? Browse other questions tagged or ask your own question.