Python批量对txt列求和# 批量对每⼀个txt的需求列求和
import pandas as pd
import os
import linecache
file=r"C:\Users\zm\"
filename=r"C:\Users\zm\Desktop\"
root=r"C:\Users\zm\Desktop\wwlln_year"
dir_name = os.listdir(root)
file_ob_list=[]
for file_name in dir_name:
filedir=root+"\\"+file_name
file_ob_list.append(filedir)
def read_data(file_path):
colume_name=['dataa','number']
ad_csv(file_path,header=None,names=colume_name)
return data
def _sum(arr,n):
return(sum(arr))
# ⽅法1
#rddata=read_data(file)
#x=rddata['number']
#n=len(x)
#ans=_sum(x,n)
#print(ans)
# ⽅法2
#sum=0
#for i in range(0,len(x)):
# sum+=x[i]
#print(sum)
writelines使用方法pythonwith open(filename,'w') as f:
for file in file_ob_list:
rddata=read_data(file)
x=rddata['number']
sum=0
for i in range(0,len(x)):
sum+=x[i]
print(sum)
f.writelines([str(sum),'\r\n'])
f.close
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论