在 Python 中读取 n 行带分隔符的 txt 文件

Read n qty rows of delimited txt file in Python

我有一个很大的 txt 文件,我想在 python 中阅读。它是制表符分隔的。我也希望能够阅读 headers。我看到了这个 Whosebug 站点,但它没有显示如何指定行的 n 数量以及确定分隔符和换行符:Read first N lines of a file in python

Pandas dataframe 将帮助您自动执行此操作。

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