gpt4 book ai didi

python - 无法将文本正确提取到列表中

转载 作者:行者123 更新时间:2023-12-01 07:58:56 25 4
gpt4 key购买 nike

我有一个文件列表,我希望从每个文件中提取文本,并将每个文件的文本放在单独的列表中。尽管如此,输出以字符串形式出现,这使我很难区分哪个文本属于哪个文件。

------------这是文件列表:

[WindowsPath('C:/Users/xxxx/Desktop/test_folder/final test.txt'),  WindowsPath('C:/Users/xxxx/Desktop/test_folder/iptest.txt'), WindowsPath('C:/Users/xxxx/Desktop/test_folder/New Text Document.txt'), WindowsPath('C:/Users/xxxx/Desktop/test_folder/test2.txt')]

------------我得到的输出是:

rgerg



egfreg



secret

dafreagr 343.23.12.53.100 aefref
secret

grre

regreg



ergre

测试正常

------------我希望输出为:

[['rgerg','egfreg','secret'],
['dafreagr 343.23.12.53.100 aefref'],
['secret','grre','regreg','ergre'],
['test is working']]

------------或者,可以使用单独的列表:

['rgerg','egfreg','secret']
['dafreagr 343.23.12.53.100 aefref']
['secret','grre','regreg','ergre']
['test is working']

-------------我有一个函数“loader()”,当前提取文件的文本:

 for items in txt_files:
for item in loader(items):
words = item
print(words)

我无法使用列表和字典获得所需的输出。不知道我应该如何处理这个问题。

更新输出:

项目输出:

C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\XXXX\Desktop\test\New Microsoft Word Document.docx
C:\Users\xxxx\Desktop\test\secretwe.docx
C:\Users\xxxx\Desktop\test\secretwe.docx
C:\Users\xxxx\Desktop\test\secretwe.docx
C:\Users\xxxx\Desktop\test\secretwe.docx
C:\Users\xxxx\Desktop\test\secretwe.docx
C:\Users\xxxx\Desktop\test\secretwe.docx
C:\Users\xxxx\Desktop\test\secretwe.docx

项目输出:

S
e
c
r
e
t












S
e
c
r
e
t

t
h
i
s

i
s

a

t
e
s
t

d
o
c
u
m
e
n
t

f
o
r

k
e
y
w
o
r
d

s
c
a
n
s
.




T
h
i
s

i
s

a

t
e
s
t
.




S
e
c
r
e
t
s
e
c
r
e
t

最佳答案

您需要声明一个结果列表,然后向其中附加数据。

例如:

result = []
for items in txt_files:
temp = []
for item in loader(items):
temp.append(item)
result.append(temp)

print(result)

根据评论进行编辑

result = []
for items in txt_files:
result.append(loader(items).splitlines())
print(result)

关于python - 无法将文本正确提取到列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55810641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com