【问题标题】:Need help getting the output of a for loop into Excel/CSV using OS.WALK需要帮助使用 OS.WALK 将 for 循环的输出转换为 Excel/CSV
【发布时间】:2020-01-22 03:49:17
【问题描述】:
在将打印输出转换为 CSV/Excel 文件方面需要帮助。
感谢您的帮助!
import pandas as pd
import os
for root, dirs, files in os.walk("C:/"):
for file in files:
if file.endswith(".xlsx"):
print(os.path.join(root, file))
#Need to print the results of this search into an excel/csv File.
【问题讨论】:
标签:
python
excel
pandas
csv
os.walk
【解决方案1】:
一种可能性是将行添加到列表中,然后从中创建数据框并将其写入文件。不确定这是否是您要找的。p>
import pandas as pd
import os
l = []
for root, dirs, files in os.walk(r"C:\"):
for file in files:
if file.endswith(".xlsx"):
l.append(os.path.join(root, file))
pd.DataFrame(l).to_excel(r"C:\Users\______\Desktop\filelist.xlsx", index=False)
如果您需要 CSV 文件,请改用 pandas.to_csv(...)。
【解决方案2】:
这个怎么样:
我们可以递归地在您的目录中搜索所有 excel 并将结果打印到数据框,
from pathlib import Path
import pandas as pd
excels = [f for f in Path(r"C:/").rglob('*.xlsx')] # might take a while.
# remove r if you're not on windows.
name = [f.stem for f in excels]
absoloute_path = [f.absolute() for f in excels]
parent = [f.parent for f in excels]
home = [f.home() for f in excels]
parents = {k.stem:'-->'.join([str(i) for i in k.parents][::-1]) for k in excels}
df = pd.DataFrame({'name' : name,
'abs' : absoloute_path,
'parent' : parent,
'home' : home})
final = pd.concat(
[
df.set_index("name"),
pd.DataFrame.from_dict(parents, orient="index", columns=["parents"]),
],
axis=1,
)
Final 将是一个数据框,其文件名、绝对路径、父级、主目录和父级结构由C:/ -- > C:/foo ---> C:/foo/bar 标识
然后您可以使用 final.to_excel 将其导出到 excel