1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python读取大文件内容_python读取大文件

python读取大文件内容_python读取大文件

时间:2021-08-21 00:24:32

相关推荐

python读取大文件内容_python读取大文件

python读取文件对各列进行索引 可以用readlines, 也可以用readline, 如果是大文件一般就用readlined={}

a_in=open("testfile.txt","r")

forlineina_in:

columnssplit=line.rstrip().split("\t")

d[columnssplit[0]]=columnssplit[1]

a_in.close()

ID_test=open("correlation.txt","r")

forlineinID_test:

s=line.rstrip().split("\t")

ifs[1]ind:

prints[0]+"\t"+d[s[1]]

ID_test.close()

##Hereisanotherexample

f=open("test.txt","r")

whileTrue:

line=f.readline()

ifline:#或者用ifline!="":

printline

else:

break

f.close()

python 还有一个pandas 主要用于大数据分析, 它与matplotlib以及 numpy 结合可以替代R语言进行统计学分析, 获取dataframe的 各行内容,可以用iterrows() 和 itertuples(), 其中 itertuples() 比 iterrows()速度更快。importpandasaspd

df1=pd.read_csv("test.txt",header=None,sep='\t')

#打印前三行

printdf1[:3]

#选取前三行的前三列,使用df.loc

df2=df1.loc[:3,[0,1,2]]

printdf2

#替换第三列的部分内容

col3=df2.apply(lambdar:r[2].replace("KH","TF"),axis=1)

#合并第一列和替换后的第三列内容

df3=pd.concat([df2[0],col3],axis=1)

printdf3

参考:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。