我正在研究NLP,需要对数据进行一些预处理。 我有两个输入文件,必须生成一个输出文件,其中第一个文件作为密钥使用这些文件的交集。
文件1 - 包含单词列表:
计算机辅助
从
该
诗
到
文件2:
0.418 0.24968 -0.41242 0.1217 0.34527 -0.044457 -0.49688 -0.17862 -0.00066023 -0.6566 0.27843 -0.14767 -0.55677 0.14658 -0.0095095 0.011658 0.10204 -0.12792 -0.8443 -0.12181 -0.016801 -0.33279 -0.1552 -0.23131 -0.19181 -1.8823 -0.76746 0.099051 -0.42125 -0.19526 4.0071 -0.18594 -0.52287 -0.31681 0.00059213 0.0074449 0.17778 -0.15897 0.012041 -0.054223 -0.29871 -0.15749 -0.34758 -0.045637 -0.44251 0.18785 0.0027849 -0.18411 -0.11514 -0.78581
的0.70853 0.57088 -0.4716 0.18048 0.54449 0.72603 0.18157 -0.52393 0.10381 -0.17566 0.078852 -0.36216 -0.11829 -0.83336 0.11917 -0.16605 0.061555 -0.012719 -0.56623 0.013616 0.22851 -0.14396 -0.067549 -0.38157 -0.23698 -1.7037 -0.86692 -0.26704 -0.2589 0.1767 3.8676 -0.1613 -0.13273 -0.68881 0.18444 0.0052464 -0.33874 -0.078956 0.24185 0.36576 -0.34727 0.28483 0.075693 -0.062178 -0.38988 0.22902 -0.21617 -0.22562 -0.093918 -0.80375
至0.68047 -0.039263 0.30186 -0.17792 0.42962 0.032246 -0.41376 0.13228 -0.29847 -0.085253 0.17118 0.22419 -0.10046 -0.43653 0.33418 0.67846 0.057204 -0.34448 -0.42785 -0.43275 0.55963 0.10032 0.18677 -0.26854 0.037334 -2.0932 0.22171 -0.39868 0.20912 -0.55725 3.8826 0.47466 -0.95658 -0.37788 0.20869 -0.32752 0.12751 0.088359 0.16351 -0.21634 -0.094375 0.018324 0.21048 -0.03088 -0.19722 0.082279 -0.09434 -0.073297 -0.064699 -0.26044
和0.26818 0.14346 -0.27877 0.016257 0.11384 0.69923 -0.51332 -0.47368 -0.33075 -0.13834 0.2702 0.30938 -0.45012 -0.4127 -0.09932 0.038085 0.029749 0.10076 -0.25058 -0.51818 0.34558 0.44922 0.48791 -0.080866 -0.10121 -1.3777 -0.10866 -0.23201 0.012839 -0.46508 3.8463 0.31362 0.13643 -0.52244 0.3302 0.33707 -0.35601 0.32431 0.12041 0.3512 -0.069043 0.36885 0.25168 -0.24517 0.25381 0.1367 -0.31178 -0.6321 -0.25028 -0.38097
我想要在新文件(文件3)中输出的内容应为:
0.418 0.24968 -0.41242 0.1217 0.34527 -0.044457 -0.49688 -0.17862 -0.00066023 -0.6566 0.27843 -0.14767 -0.55677 0.14658 -0.0095095 0.011658 0.10204 -0.12792 -0.8443 -0.12181 -0.016801 -0.33279 -0.1552 -0.23131 -0.19181 -1.8823 -0.76746 0.099051 -0.42125 -0.19526 4.0071 -0.18594 -0.52287 -0.31681 0.00059213 0.0074449 0.17778 -0.15897 0.012041 -0.054223 -0.29871 -0.15749 -0.34758 -0.045637 -0.44251 0.18785 0.0027849 -0.18411 -0.11514 -0.78581
要0.68047 -0.039263 0.30186 -0.17792 0.42962 0.032246 -0.41376 0.13228 -0.29847 -0.085253 0.17118 0.22419 -0.10046 -0.43653 0.33418 0.67846 0.057204 -0.34448 -0.42785 -0.43275 0.55963 0.10032 0.18677 -0.26854 0.037334 -2.0932 0.22171 -0.39868 0.20912 -0.55725 3.8826 0.47466 -0.95658 -0.37788 0.20869 -0.32752 0.12751 0.088359 0.16351 -0.21634 -0.094375 0.018324 0.21048 -0.03088 -0.19722 0.082279 -0.09434 -0.073297 -0.064699 -0.26044
以下代码运行时没有任何错误,但我得到的输出文件为空:
f1 = open('input_key.txt', 'r')
f2 = open('input_file.txt', 'r')
f3 = open('output_file.txt', 'w')
for word in f1.readlines():
for line in f2.readlines():
if word is line.strip().split()[0]:
f3.write(line)
f1.close()
f2.close()
f3.close()
我无法理解这里有什么问题。任何帮助赞赏。 file2和file3之间没有多余的行。我刚刚添加了这些以使问题可读。
更新
感谢评论,我知道if语句正在评估为false。有没有办法克服这个或其他替代方案来执行我的任务?
答案 0 :(得分:2)
关键字is
是身份运算符,检查2个元素是否是相同的身份
==
是等于逻辑运算符
if word is line.strip().split()[0]:
将其更改为
if word == line.strip().split()[0]:
答案 1 :(得分:2)
这将做你想做的事情:
f1 = open('input_key.txt', 'r')
f2 = open('input_file.txt', 'r')
f3 = open('output_file.txt', 'a')
for word in f1.readlines():
for line in f2.readlines():
if line != '\n' and word.strip() == line.strip().split()[0]:
f3.write(line)
f2.seek(0)
f1.close()
f2.close()
f3.close()
您需要使用readlines
f2.seek(0)
的光标位置
我还会将output_file.txt
打开为a
(追加),您可以删除脚本开头的output_file.txt
,以便每次运行时清除它:< / p>
import os
os.remove("output_file.txt")
我也会==
代替is
,is
会测试两个对象是否相同,而不是等于其他东西
编辑:关于编写清洁代码的一些提示,我会在下面看看关于list comprehension
的关于wiesion的答案
答案 2 :(得分:2)
我刚刚复制了你的文件,并按照我的要求编写代码:
with open("words.txt", "r") as word_file:
words = [word.strip() for word in word_file.read().splitlines() if word.strip()]
with open("feed.txt", "r") as feed_file:
lines = [line.strip() for line in feed_file.read().splitlines() if line.strip()]
with open('result.txt', 'w') as result_file:
result_file.write("\n".join([line for line in lines if line.split()[0] in words]))
当然我在这里做了很多list comprehensions以避免所有嵌套循环。
如果您的单词和输入文件很大,那么您应该避免将整个文件读入内存并进行理解(感谢@Bayko提醒),您应该切换到:
words = []
with open("words.txt", "r") as word_file:
# This reads the words file line by line instead of reading the entire file
for word in word_file:
word = word.strip()
if word:
words.append(word)
with open('result.txt', 'w') as result_file:
with open("feed.txt", "r") as feed_file:
# This reads the input file line by line instead of reading the entire file
for line in feed_file:
line = line.strip()
if not line:
continue
if line.split()[0] in words:
result_file.write(line + "\n")
当我在本地运行您的代码时:
if word is line.strip().split()[0]:
IndexError:列表索引超出范围
由于空行而发生此错误 - 但最重要的是,您仍然遇到f2.readlines()
- 您永远不会f2.seek(0)
重置位置,而==
与is
不一样f1 = open('words.txt', 'r')
f2 = open('feed.txt', 'r')
f3 = open('result.txt', 'w')
for word in f1.readlines():
word = word.strip()
if not word:
continue
for line in f2.readlines():
line = line.strip()
if not line:
continue
if word == line.split()[0]:
f3.write(line + "\n")
f2.seek(0)
f1.close()
f2.close()
f3.close()
(参见@ Atterson的回答)。修复代码中的问题,如下所示:
result.txt
使用这两个脚本{{1}}看起来像
0.418 0.24968 -0.41242 0.1217 0.34527 -0.044457 -0.49688 -0.17862 -0.00066023 -0.6566 0.27843 -0.14767 -0.55677 0.14658 -0.0095095 0.011658 0.10204 -0.12792 -0.8443 -0.12181 -0.016801 -0.33279 -0.1552 -0.23131 -0.19181 -1.8823 -0.76746 0.099051 -0.42125 -0.19526 4.0071 -0.18594 -0.52287 -0.31681 0.00059213 0.0074449 0.17778 -0.15897 0.012041 -0.054223 -0.29871 -0.15749 -0.34758 -0.045637 -0.44251 0.18785 0.0027849 -0.18411 -0.11514 -0.78581
至0.68047 -0.039263 0.30186 -0.17792 0.42962 0.032246 -0.41376 0.13228 -0.29847 -0.085253 0.17118 0.22419 -0.10046 -0.43653 0.33418 0.67846 0.057204 -0.34448 -0.42785 -0.43275 0.55963 0.10032 0.18677 -0.26854 0.037334 -2.0932 0.22171 -0.39868 0.20912 -0.55725 3.8826 0.47466 -0.95658 -0.37788 0.20869 -0.32752 0.12751 0.088359 0.16351 -0.21634 -0.094375 0.018324 0.21048 -0.03088 -0.19722 0.082279 -0.09434 -0.073297 -0.064699 -0.26044
答案 3 :(得分:0)
第二个文件中有三个新行。拆分后,需要对阵列进行检查以检查阵列是否有任何元素。这解决了这个问题。或者尝试捕获IndexError,你应该很好