python 正确下载ftp文件

发布于 2025-01-13 17:37:29 字数 843 浏览 3 评论 0原文

我正在尝试在内存中写入一个类似对象的文件。 我在 ftp 上下载了一个文件,当我尝试使用 read 方法将其转换为 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 字符转换为字符串,并且是 ' 字符逃走了? 如何正确获取文件内容?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋意浓 2025-01-20 17:37:29

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文