![](/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.