![](/img/trans.png)
[英]How can I open a csv file in python, and read one line at a time, without loading the whole csv file in memory?
[英]Edit a .csv without read the whole file (python)
我目前正在使用数据清理。 我需要清理一个大的带有几行标题的csv文件,最后一行需要切掉。 有什么办法可以将行切碎而不加载整个文件?
从另一个类似的线程中获取了它,它应该可以解决您的问题:
for line in reversed(open("filename").readlines()):
print line.rstrip()
在Python 3中:
for line in reversed(list(open("filename"))):
print(line.rstrip())
Python是面向行的,直接进入解释器。
我认为,执行此操作的最佳方法是sys.call
并使用更适合文件级操作的外部程序。 这不是作弊,python是一种很棒的脚本语言。 您应该使用Unix工具。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.