我有以下python脚本打开一个CSV文件并执行一些位来重新创建列和切片列表,最后输出为CSV。
我希望脚本能够存储目录中的所有当前.CSV文件并执行操作并输出最终的CSV文件。
仅供参考:我试图避开PANDAS。
import csv
projects = []
count = 0
with open('timesheets.csv') as csvfile:
timesheets = csv.reader(csvfile, delimiter=',')
for rows in timesheets:
# IF statement will run until count is more than 3, then move out of IF statement and print remaining rows.
if count < 3:
count += 1
continue
# Columns 1,8,9,13,15,18
columns1 = rows[1:2]
columns8_9 = rows[8:10]
columns13 = rows[13:14]
columns15 = rows[15:15]
columns18 = rows[18:19]
project = columns1 + columns8_9 + columns13 + columns15 + columns18
# Append each line as a seperate list to projects list. You end up with multiple lists within in a list.
projects.append(project)
# Remove the last list in projects list, since being empy and causes errors.
projects = projects[:-1]
newlist = []
# Remove the first 6 characters from each line[1] & line[4]
for lists in projects:
# Remove the first 6 characters from each line[1]
engineer = lists[1]
engineer = engineer[8:]
lists[1] = engineer
# Remove the first 6 characters from each line[3]
employee = lists[3]
employee = employee[8:]
lists[3] = employee
newlist.append(lists)
# Change the first list to the following list, which effectively changes the column names.
newlist[0] = ['Project Name', 'Line Manager', 'Element', 'Employee', 'Hours']
writer = csv.writer(open('output.csv', 'w'))
writer.writerows(newlist)
答案 0 :(得分:0)
将csv列表放在列表中并进行迭代。这将对您有所帮助: -
import glob
files_list = glob.glob("path/*.csv")
for file in files_list:
#your remaining code goes here...