简体   繁体   English

在Python中截断较大的csv文件

[英]Truncating a large csv-file in Python

I have a csv file which is too large to completely fit into my laptop's memory (about 10GB). 我的csv文件太大,无法完全放入笔记本电脑的内存中(约10GB)。 Is there a way to truncate the file such that only the first n entries are saved in a new file? 有没有一种方法可以截断文件,以便仅将前n个条目保存在新文件中? I started by trying 我开始尝试

df = pandas.read_csv("path/data.csv").as_matrix()

but this doesn´t work since the memory is too small. 但这不起作用,因为内存太小。

Any help will be appreciated! 任何帮助将不胜感激!

Leon 莱昂

Use nrows : 使用nrows

df = pandas.read_csv("path/data.csv", nrows=1000)

The nrows docs say: nrows文档说:

Number of rows of file to read. 要读取的文件行数。 Useful for reading pieces of large files 对于读取大文件很有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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