【发布时间】:2019-12-01 10:13:13
【问题描述】:
我正在尝试从许多 docs 文件中提取数据并将它们附加到数据框中。
我编写的代码在处理单个文件时效果很好,但我似乎无法将更多文件附加到数据框中。
import re
import docx2txt
import pandas as pd
import glob
df2=pd.DataFrame()
appennded_data=[]
for file in glob.glob("*.docx"):
text = docx2txt.process(file)
a1=text.split()
d2=a1[37]
doc2=re.findall("HB0....",text)
units2=re.findall("00[0-9]...",text)
df2['Units']=units2
df2['Doc']=doc2[0]
df2['Date']=d2
df2
这给出了一个错误 “值的长度与索引的长度不匹配”
预期输出-
来自 docx1:(我得到的)
Units | Doc | Date
001 | HB00001 | 23/4/12
002 | HB00001 | 23/4/12
003 | HB00001 | 23/4/12
004 | HB00001 | 23/4/12
005 | HB00001 | 23/4/12
来自 docx2:
Units | Doc | Date
010 | HB00002 | 2/6/16
011 | HB00002 | 2/6/16
最终输出:
Units | Doc | Date
001 | HB00001 | 23/4/12
002 | HB00001 | 23/4/12
003 | HB00001 | 23/4/12
004 | HB00001 | 23/4/12
005 | HB00001 | 23/4/12
010 | HB00002 | 2/6/16
011 | HB00002 | 2/6/16
任何帮助将不胜感激
【问题讨论】:
-
你能分享一下你的word doc文件的格式吗?是桌子还是别的什么。如果我们了解这一点,我们会为您提供更好的帮助。
-
你能检查a1 & doc2的内容看指定的索引是否有效吗?
-
嗨,它只是一个包含文本的 docx 文件。没有表格或列表或任何东西。文本处理效果很好,我可以只用一个文件创建一个数据框。我只是无法为其他 doc 文件附加数据框。
标签: python pandas loops dataframe