我正在遍历目录中的文件,并希望将文件名和从文件中提取的一些东西保存在同一只熊猫数据框中。在浏览目录中的所有文件时,如何将txt文件的名称保存在列表中(然后将其作为单独的列插入pandas数据框)?
这是我的代码的一部分:
columns_df = ['file', 'stuff']
df_stuff = pd.DataFrame(columns = columns_df)
filenamelist = []
stufflist = []
os.chdir(r'path\to\directory')
for file in glob.glob('*.txt'):
# Extract some stuff from file and append to stufflist (DONE)
# Save filename in the filenamelist (THE PROBLEM)
df_stuff['stuff'] = stufflist
df_stuff['file'] = filenamelist
您需要此功能吗?
for file in glob.glob('*.txt'):
filenamelist.append(file)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句