python 正确下载 ftp 文件

python download ftp file properly

我正在尝试在内存中写入一个类似对象的文件。 我在 ftp 上下载了一个文件,当我尝试使用读取方法将它转换为 StringIO 或 BytesIO 对象时,我得到了这个:

"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'

这是代码:

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

为什么 \r\n 个字符转换为字符串并且 ' 字符被转义? 如何正确获取文件内容?

我发现正确的编码格式是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