标签:int 批量 none txt def list eve return com
# 批量对每一个txt的需求列求和
import pandas as pd
import os
import linecache
file=r"C:\Users\zm.com\Desktop\201601.txt"
filename=r"C:\Users\zm.com\Desktop\every_year.txt"
root=r"C:\Users\zm.com\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‘]
data=pd.read_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)
with 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
标签:int 批量 none txt def list eve return com
原文地址:https://www.cnblogs.com/stelliformzm/p/12952411.html