0
私は、1つのCSVファイルを開き、いくつかのビットを実行して列とスライスリストを再作成し、最終的にCSVに出力する次のpythonスクリプトを取得しました。PythonはすべてのCSVファイルに対してロジックを実行します
ディレクトリ内のすべての現在の.CSVファイルを1つのスクリプトにして、アクションを実行して最終的なCSVファイルを出力します。
FYI:私は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)
...とは何ですか? –
'FYI:PANDASを避けようとしています.'あなたの損失... –
' pandas'を避けようとしているのはなぜですか?インストールに問題はありますか?そうでない場合は、絶対に使用する必要があります。 – asongtoruin