![](/img/trans.png)
[英]How to sequentially read all the files in a directory and export the contents in Python?
[英]How can I read the contents of all the files in a directory with pandas?
我有一個包含大量.txt
文件的文件夾。 如何讀取文件夾中的所有文件並使用 pandas 獲取它們的內容?。 我嘗試了以下內容:
import pandas as pd
list_=pd.read_csv("/path/of/the/directory/*.txt",header=None)
print list_
像這樣的東西:
import glob
l = [pd.read_csv(filename) for filename in glob.glob("/path/*.txt")]
df = pd.concat(l, axis=0)
你如果要忽略它看一看在skiprows選項,考慮到頭部,例如read_csv 。
我在我的項目中使用它來合並 csv 文件
import pandas as pd
import os
path = "path of the file"
files = [file for file in os.listdir(path) if not file.startswith('.')]
all_data = pd.DataFrame()
for file in files:
current_data = pd.read_csv(path+"/"+file , encoding = "ISO-8859-1")
all_data = pd.concat([all_data,current_data])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.