使用 if 循环 python 在我的列表中保存文件名时出现问题
Problem in saving filenames in my list using if loop python
我写了一段代码,基本上从所有文件夹中选择第一个 pdf,然后从所有 pdf 中提取文本数据。
我将这些提取的 pdf 文本数据保存到数据框中。我还想将 pdf 文件的名称保存到我的数据框中,问题是,它一直在我的数据框中写入最近的 pdf 文件名,而忽略其余的 pdf 文件名。
这是我的代码:
folders = ['F:/a/b/input_data/Sample documents/xyz/',
'F:/a/b/input_data/Sample documents/abc/',
'F:/a/b/input_data/Sample documents/pqr/']
allmypdfs = []
for folder in folders:
alllfiles = os.listdir(folder)
firstpdfs = ""
for i in alllfiles:
if '.pdf' or '.PDF' in i:
firstpdfs = i
print('PDF-Names--', firstpdfs)
break
with open(folder + firstpdfs, 'rb') as fh:
for page in PDFPage.get_pages(fh, caching=True, check_extractable=True):
page_interpreter.process_page(page)
text = fake_file_handle.getvalue()
texts = re.sub(r'[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff]', '', text) # to remove \x0c escape chars
allmypdfs.append(texts)
# dataframe creation
data = {'PDF NAME': firstpdfs, 'Text Data': [allmypdfs]}
df = pd.DataFrame(data)
df1 = df.explode('Text Data')
print(df1.head(50))
我已经尝试创建一个数据框来在 if 循环中捕获 pdf 文件名,然后将其附加到文本数据,但这并没有得到正确的输出(因为它在 if 循环中每次都会创建一个新的数据框).
我也试过先创建一个空列表,然后附加文件名,但这在输出中给了我一个 None 。
像这样
allmypdfs = []
files = []
for folder in folders:
alllfiles = os.listdir(folder)
firstpdfs = ""
for i in alllfiles:
if '.pdf' or '.PDF' in i:
firstpdfs = i
print('PDF-Names--', files.append(firstpdfs))
break
with open(folder + firstpdfs, 'rb') as fh:
for page in PDFPage.get_pages(fh, caching=True, check_extractable=True):
page_interpreter.process_page(page)
text = fake_file_handle.getvalue()
texts = re.sub(r'[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff]', '', text) # to remove \x0c escape chars
allmypdfs.append(texts)
这给了我类似 PDF-Names-- None
的输出
我想要的输出应该类似于
0 PDF NAME Text Data
0 1.pdf ofkndv....
1 2.pdf dfgnnrjon...
我得到的输出是:
0 PDF NAME Text Data
0 1.pdf ofkndv....
1 1.pdf dfgnnrjon...
请帮助我了解如何使其以正确的方式工作。
由于来自 list
对象 returns None
的方法 append()
,print('PDF-Names--', files.append(firstpdfs))
必须在您的控制台上打印 PDF-Names-- None
。
我猜,你的意图是这样的:
# print file name of first PDF found in this folder
print('PDF-Names--', firstpdfs)
# append file name to list of files
files.append(firstpdfs)
关于你的数据框,你在循环后构建字典,其中只有 firstpdfs
是你上次访问的文件夹中第一个 PDF 的名称。
您正在从单个字典 ({'PDF NAME': firstpdfs, 'Text Data': [allmypdfs]}
) 创建 table。但是,它的目的是从 词典列表 中创建一个。
使用此架构
data_list = []
for folder_name in folders:
# TODO: first PDF files in the directory <folder_name> and return <file_name>
with open(file_name, 'rb') as fh:
# TODO: extract text from file and return as binary string in variable <text>
# store data in a dictionary and append to the list
data_list.append({'PDF NAME': file_name, 'Text Data': text})
# convert list of dictionaries to pandas.DataFrame object
data = pd.DataFrame(data_list)
您可能希望更简洁的命名和简洁的写作(写一些小函数,因为它们更容易维护和调试)以避免失去对代码的概述
我写了一段代码,基本上从所有文件夹中选择第一个 pdf,然后从所有 pdf 中提取文本数据。 我将这些提取的 pdf 文本数据保存到数据框中。我还想将 pdf 文件的名称保存到我的数据框中,问题是,它一直在我的数据框中写入最近的 pdf 文件名,而忽略其余的 pdf 文件名。
这是我的代码:
folders = ['F:/a/b/input_data/Sample documents/xyz/',
'F:/a/b/input_data/Sample documents/abc/',
'F:/a/b/input_data/Sample documents/pqr/']
allmypdfs = []
for folder in folders:
alllfiles = os.listdir(folder)
firstpdfs = ""
for i in alllfiles:
if '.pdf' or '.PDF' in i:
firstpdfs = i
print('PDF-Names--', firstpdfs)
break
with open(folder + firstpdfs, 'rb') as fh:
for page in PDFPage.get_pages(fh, caching=True, check_extractable=True):
page_interpreter.process_page(page)
text = fake_file_handle.getvalue()
texts = re.sub(r'[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff]', '', text) # to remove \x0c escape chars
allmypdfs.append(texts)
# dataframe creation
data = {'PDF NAME': firstpdfs, 'Text Data': [allmypdfs]}
df = pd.DataFrame(data)
df1 = df.explode('Text Data')
print(df1.head(50))
我已经尝试创建一个数据框来在 if 循环中捕获 pdf 文件名,然后将其附加到文本数据,但这并没有得到正确的输出(因为它在 if 循环中每次都会创建一个新的数据框). 我也试过先创建一个空列表,然后附加文件名,但这在输出中给了我一个 None 。 像这样
allmypdfs = []
files = []
for folder in folders:
alllfiles = os.listdir(folder)
firstpdfs = ""
for i in alllfiles:
if '.pdf' or '.PDF' in i:
firstpdfs = i
print('PDF-Names--', files.append(firstpdfs))
break
with open(folder + firstpdfs, 'rb') as fh:
for page in PDFPage.get_pages(fh, caching=True, check_extractable=True):
page_interpreter.process_page(page)
text = fake_file_handle.getvalue()
texts = re.sub(r'[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff]', '', text) # to remove \x0c escape chars
allmypdfs.append(texts)
这给了我类似 PDF-Names-- None
的输出
我想要的输出应该类似于
0 PDF NAME Text Data
0 1.pdf ofkndv....
1 2.pdf dfgnnrjon...
我得到的输出是:
0 PDF NAME Text Data
0 1.pdf ofkndv....
1 1.pdf dfgnnrjon...
请帮助我了解如何使其以正确的方式工作。
由于来自 list
对象 returns None
的方法 append()
,print('PDF-Names--', files.append(firstpdfs))
必须在您的控制台上打印 PDF-Names-- None
。
我猜,你的意图是这样的:
# print file name of first PDF found in this folder
print('PDF-Names--', firstpdfs)
# append file name to list of files
files.append(firstpdfs)
关于你的数据框,你在循环后构建字典,其中只有 firstpdfs
是你上次访问的文件夹中第一个 PDF 的名称。
您正在从单个字典 ({'PDF NAME': firstpdfs, 'Text Data': [allmypdfs]}
) 创建 table。但是,它的目的是从 词典列表 中创建一个。
使用此架构
data_list = []
for folder_name in folders:
# TODO: first PDF files in the directory <folder_name> and return <file_name>
with open(file_name, 'rb') as fh:
# TODO: extract text from file and return as binary string in variable <text>
# store data in a dictionary and append to the list
data_list.append({'PDF NAME': file_name, 'Text Data': text})
# convert list of dictionaries to pandas.DataFrame object
data = pd.DataFrame(data_list)
您可能希望更简洁的命名和简洁的写作(写一些小函数,因为它们更容易维护和调试)以避免失去对代码的概述