2014-09-03 44 views
0

我想爲我自己的目的使用Python腳本here。我不是Python傢伙,所以希望有人能看到我的錯誤。使用正則表達式將文件夾內容寫入CSV

下面的腳本不會出錯。我的CSV創建時沒有值。我有沒有加入問題?我期待將數據寫入CSV。

# import the standard libraries you'll need 
import os # https://docs.python.org/2/library/os.html 
import re # https://docs.python.org/2/library/re.html 

# this function will walk your directories and output a list of file paths 
def getFilePaths(directory): 
    file_paths = [] 
    for root, directories, files in os.walk(directory): 
     for filename in files: 
      filepath = os.path.join(root, filename) 
      file_paths.append(filepath) 
    return file_paths 

audio_file_paths = getFilePaths("Z:\Dropbox\Apps\DirScan\files") 
output_to_csv = []; 

for audio_file in audio_file_paths: 
    base_path, fname = os.path.split(audio_file) 

    reg_ex = re.compile("^(.*) - (.*) - (.*).mp3$"); 

    # now apply the compiled regex to each path 
    name_components = reg_ex.match(fname); 

    output_to_csv.append("{0},{1}".format(",".join(name_components), base_path)); 

#create the file, making sure the location is writeable 
csv_doc = open("database.csv", "w"); 

# now join all the rows with line breaks and write the compiled text to the file 
csv_doc.write('\n'.join(output_to_csv)); 


#close your new database 
csv_doc.close() 
+1

你想找到歌曲的名字? – Kasramvd 2014-09-03 23:09:22

+0

是的。理想的情況是eyeD3這樣的MP3標籤可以工作。但文件名遵循嚴格的格式,因此可以從中提取此表/ CSV。 – 2014-09-03 23:16:18

回答

1

當我運行代碼,我得到這個錯誤:

Traceback (most recent call last): 
    File "x.py", line 29, in <module> 
    output_to_csv.append("{0},{1}".format(",".join(name_components), base_path)); 
TypeError 

因爲name_components是一個正則表達式Match對象,不作爲參數傳遞給join工作。您需要替換:

",".join(name_components) 

有了:

",".join(name_components.groups()) 

做出這樣的轉變後,我可以看到CSV文件被正確寫入。

另一個小問題:你不需要在python的一行末尾使用分號。

+0

謝謝,謝謝!僅供參考,我不得不在文件路徑中將反斜槓更改爲正斜槓。 – 2014-09-04 00:49:18