给了10个csv文件记录考勤,要求编写程序输出全勤名单。难道要一个个的读取csv文件吗,open()…
放到同一个文件夹下,然后读取该文件夹,获取所有的csv文件,再一个个遍历读取
把上面的内容全部放在一个for循环中,
直接放具体的代码:
import csv
import os
source_path = './excel_data/'
save_path = './csv_data/'
CsvFile = os.listdir(source_path)
if not os.path.exists(save_path):
os.mkdir(save_path)
for leng in range(len(CsvFile)):
with open(source_path+CsvFile[leng],'rt',encoding='utf-8') as histfile:
hist_reader = csv.reader(histfile)
hist_column =[row[1] for row in hist_reader]
# print(hist_column)
with open(source_path+CsvFile[leng],'rt',encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
column =[row[0] for row in reader]
# hist_number = [row[1] for row in reader]
# print(len(hist_number))
for i in range(len(column)):
if('psec' in column[i]):
column[i] = column[i].replace('psec', '')
elif ('fsec' in column[i]):
column[i] = column[i].replace('fsec', '')
column[i] = float(column[i])*0.001
column[i] = str(column[i])
elif('sec' in column[i] and 'E' in column[i]):
column[i] = str(0)
else:
column[i] = column[i]
# print(column[i])
with open(save_path+CsvFile[leng],'wt',encoding='utf-8',newline='') as newfile:
writer = csv.writer(newfile)
for col,hist in zip(column, hist_column):
writer.writerow([col, hist])
newfile.close()
print(str(leng)+': Finished')
每一个具体的csv的名字由两部分组成: 文件夹名字+csv名字
写个for循环试试