我正在嘗試將 .xls 檔案串列連接到 .csv:
import glob
import pandas as pd
file_xls = glob.glob(location "\*.xls")
print(file_xls)
read_file = pd.read_excel(location file_xls)
但我對串列的連接有一個錯誤:
TypeError: can only concatenate str (not "list") to str
有沒有一種特定的方法來連接這個串列?提前致謝!
目標:在串列的幫助下將 .xls 檔案融合到 .csv 中
uj5u.com熱心網友回復:
我認為你應該使用而不是連接字串os.path.join():
file_xls = glob.glob(location "\*.xls")
將回傳指定根目錄中所有檔案名的串列(由用戶定義為location)
files = [pd.read_excel(os.path.join(location,x)) for x in file_xls]
以 pandas DataFrames 形式回傳所有檔案的串列。
您可以使用 pd.concat() 連接它們,并使用df.to_csv()將它們輸出為 csv
output = pd.concat(files)
output.to_csv(desired_output_path)
您也可以使用以下命令將所有內容都單行:
pd.concat([pd.read_excel(os.path.join(location,x)) for x in glob.glob(location "\*.xls")]).to_csv(desired_output_path)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/524033.html
