在test01.txt中
are
lol
test
hello
next
在text02.txt中
lol : positive
next : objective
sunday! : objective
are : objective
you : objective
going? : neutral
mail : objective
我的代码:
file1 = open('C://Users/Desktop/test01.txt')
file2 = open('C://Users/Desktop/test02.txt')
rfile1 = file1.readlines()
rfile2 = file2.read()
for test2 in rfile2.split("\n"):
testt2 = test2.split("\t")
for test1 in rfile1:
slsw1 = test1.split()
for wttest2 in testt2:
sttestt2 = wttest2.split(" ")
if sttestt2[0] in slsw1[0]:
sttestt2[0] = sttestt2[0].replace(sttestt2[0], "")
print sttestt2[0], ":", sttestt2[2]
预期结果:
: positive
: objective
sunday! : objective
: objective
you : objective
going? : neutral
mail : objective
我试图用空格替换“test02.txt”中的同一个单词,并打印出来查看结果,但我只得到了打印出来的空格。我想按预期结果打印所有结果。你知道吗
我错过什么了吗?有什么建议吗?你知道吗
此外,您还应该考虑学习一些更好的命名方法。
rfile
除了它与文件相关之外,实际上什么都没说。我宁愿用file_content
或file_lines
左右。你知道吗另外:
test2, testt2, test1, slsw1, wttest2, sttestt2
。。。什么?你知道吗试着命名你的变量,这样名字就可以告诉你变量的用途,这对你自己和我们来说都会容易得多。:)
相关问题 更多 >
编程相关推荐