2017-10-30 13 views
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) 
+1

...とは何ですか? –

+2

'FYI:PANDASを避けようとしています.'あなたの損失... –

+0

' pandas'を避けようとしているのはなぜですか?インストールに問題はありますか?そうでない場合は、絶対に使用する必要があります。 – asongtoruin

答えて

0

リストにcsvのリストを入れて繰り返します。これはあなたを助けます: -

import glob 
files_list = glob.glob("path/*.csv") 
for file in files_list: 
    #your remaining code goes here... 
関連する問題