在Python的linux中使用windows newline有效地读取csv文件 [英] Efficiently reading a csv file with windows newline on linux in Python

查看:326
本文介绍了在Python的linux中使用windows newline有效地读取csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是在窗口下工作以逐行读取csv文件。

The following is working under windows for reading csv files line by line.

f = open(filename, 'r')

for line in f:

虽然将csv文件复制到一个linux服务器,它失败。

Though when copying the csv file to a linux server, it fails.

应该提到的性能是一个问题,因为csv文件是巨大的。因此,我担心使用像strip之类的字符串复制。

It should be mentioned that performance is an issue as the csv files are huge. I am therefore concerned about the string copying when using things like strip.

推荐答案

Python内置支持Windows,Linux和Mac结尾:

Python has builtin support for Windows, Linux and Mac line endings:

f = open(filename, 'rtU')

for line in f:
    ...

如果你真的想要不要慢字符串操作,文件之前处理它们。您可以使用dos2unix(可以在Debian软件包tofrodos中找到)或(更容易)使用FTP文本模式,这应该自动执行。

If you really want don't want slow string operations, you should strip the files before processing them. You can either use dos2unix (can be found in the Debian package "tofrodos") or (easier) use FTP text mode which should do that automatically.

这篇关于在Python的linux中使用windows newline有效地读取csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆