python读取大文件内容_python读取大文件
python讀取文件對各列進行索引 可以用readlines, 也可以用readline, 如果是大文件一般就用readlined={}
a_in?=?open("testfile.txt",?"r")
for?line?in?a_in:
columnssplit?=?line.rstrip().split("\t")
d[columnssplit[0]]=columnssplit[1]
a_in.close()
ID_test?=?open("correlation.txt",?"r")
for?line?in?ID_test:
s?=?line.rstrip().split("\t")
if?s[1]?in?d:
print?s[0]+"\t"+d[s[1]]
ID_test.close()
##?Here?is?another?example
f?=?open("test.txt",?"r")
while?True:
line?=?f.readline()
if?line:???#??或者用?if?line?!=?"":
print?line
else:
break
f.close()
python 還有一個pandas 主要用于大數據分析, 它與matplotlib以及 numpy 結合可以替代R語言進行統計學分析, 獲取dataframe的 各行內容,可以用iterrows() 和 itertuples(), 其中 itertuples() 比 iterrows()速度更快。import?pandas?as?pd
df1?=?pd.read_csv("test.txt",?header?=?None,?sep='\t')
#?打印前三行
print?df1[:3]
#?選取前三行的?前三列,?使用df.loc
df2?=?df1.loc[:3,?[0,?1,?2]]
print?df2
#?替換第三列的部分內容
col3?=?df2.apply(lambda?r:?r[2].replace("KH","TF"),?axis=1)
#?合并第一列和替換后的第三列內容
df3?=?pd.concat([df2[0],?col3],?axis?=?1)
print?df3
參考:
總結
以上是生活随笔為你收集整理的python读取大文件内容_python读取大文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 二分法调试代码,Pytho
- 下一篇: opencv java ubuntu_U