简体   繁体   中英

Edit a .csv without read the whole file (python)

I am currently working with data cleaning. I need to clean a big csv file with a few head rows and the last row need to be chopped off. Is there any way I can just chopped the rows without load the whole file?

Grabbed this from another similar thread, it should resolve your issue:

for line in reversed(open("filename").readlines()):
    print line.rstrip()

And in Python 3:

for line in reversed(list(open("filename"))):
    print(line.rstrip())

Python is line oriented, right down into the interpreter.
I think that the best way to do this would be to sys.call and using an external program that is better suited for file level manipulations. This is not cheating, python is a great scripting language. You should use the unix tools.

Remove lines from file with head, tail, or sed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM