阅读Python中分隔的文本文件的n个数量的行

问题描述 投票:0回答:1

我有,我想在python读大的txt文件。它的制表符分隔。我希望能够读取头为好。我看到这个计算器网站,但它并不显示如何既指定行的N个数量以及确定分隔符,换行符:Read first N lines of a file in python

python text-files delimiter openrowset structured-data
1个回答
1
投票

熊猫数据框中会自动帮你做到这一点。

import pandas as pd
df = pd.read_csv(myfile,sep='\t')
df.head(n=5)  # for the 5 first lines of your file

欲了解更多信息,请参阅https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html#pandas.read_csv

© www.soinside.com 2019 - 2024. All rights reserved.