假设我有一个文件 test.txt
包含:
1:text1.txt
2:text2.txt
text1.txt 包含:
I am a good person
text2.txt 包含:
Bla bla
我想输出:
I 1
Bla 2
am 1
bla 2
good 1
a 1
person 1
就像我想输出文件中每个单词的文件索引。我会发布我的代码,但它太难看了而且离解决方案还很远。我是 python 新手,所以请友善。没有指定的输出顺序,我提到的示例输出完全是随机的,只是为了让您了解我正在寻找的内容。
这是我的代码
`with open("text.txt", "r") as f: 文本=f.readlines()
for line in text:
splitted=line.split(":")
splitsplit=splitted[1].split("\n")
files=splitsplit[0]
splittedindicies=splitted[0].split("\n")
indicies=splittedindicies[0]
print indicies[0]
files_list=list(files)
files_l=files.split(" ")
for x in files_l:
fileshandle=open(x,"r")
read=fileshandle.readlines()
for y in read:
words=y.split(" ")
words.sort()
for j in words:
print j `
我的输出是:
1
I
am
a
good<br/>
person
2
Bla
bla
再说一次,请客气一点,我是 R 程序员,第一次接触 Python。
最佳答案
您应该在这里尝试一些正则表达式配方:
当你注释掉时:
how can I store the output
你的输出是 dict 的值,你可以对它们进行操作。
import re
track={}
pattern=r'(\d):?(\w+\.txt)'
with open('test.txt','r') as file_name:
for line in file_name:
match=re.finditer(pattern,line)
for finding in match:
with open(finding.group(2)) as file_name_2:
for item in file_name_2:
track[int(finding.group(1))]=item.split()
for key,value in track.items():
for item in value:
print(key,item)
输出:
1 I
1 am
1 a
1 good
1 person
2 Bla
2 bla
关于python - 如何在python中以这种格式输出文件名及其单词内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47196883/