[英]Trying to Compare a dictionary to a text file
I am trying out python for a simple web scraper that checks a website for names, puts them all in a dictionary with the link, then compares the names to a list I all ready have saved.我正在尝试使用 python 来创建一个简单的网络爬虫,它检查网站的名称,将它们全部放入带有链接的字典中,然后将名称与我都准备好保存的列表进行比较。 The problem I am stuck on is trying to make a new dictionary with the names from the website that are not in the saved list.
我遇到的问题是尝试使用网站上不在保存列表中的名称制作新词典。
together = dict(zip(names, links))
final_dict = {}
for k, v in together.items():
with open('file.txt', 'r') as f:
file_lines = f.readlines()
if k in file_lines:
print('All ready have that name.')
pass
else:
print(k, v)
final_dict.update({k, v})
f.close()
The saved file only has the k
value of the together
dictionary.保存的文件只有
together
字典的k
值。
Try to change尝试改变
file_lines = f.readlines()
to到
file_lines = f.read().splitlines()
The former one keeps the newline \\n
at the end of the line.前一个将换行符
\\n
保留在行尾。
In addition final_dict.update({k, v})
should probably be final_dict.update({k: v})
and as others already pointed out, you may have an issue with your indentation.此外
final_dict.update({k, v})
应该是final_dict.update({k: v})
并且正如其他人已经指出的那样,您的缩进可能有问题。
Your algorithm isn't very efficient.你的算法效率不高。 For each item in
together
, it reads the entire file.对于
together
每个项目,它读取整个文件。 If you handle each line in the file as you encounter it, it's much faster:如果您在遇到文件时处理文件中的每一行,速度会快得多:
together = dict(zip(names, links))
with open('file.txt', 'r') as f:
for line in f:
if line in together:
del together[line]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.