[英]how do I use the line I get from the file in equation
I am processing on json files with python programming.我正在使用 python 编程处理 json 文件。 I want to compare the data from json file with the lines in file.txt and get the output according to the result.
我想将 json 文件中的数据与 file.txt 中的行进行比较,并根据结果获取输出。
what should I replace with filex[0] in the code?我应该用代码中的 filex[0] 替换什么?
filename = 'paf.json'
with open(filename, 'r') as f:
for line in f:
if line.strip():
tweet = json.loads(line)
file1=open("file.txt","r")
filex=file1.readlines()
for linex in filex:
lines=linex
for char in tweet:
if str(tweet['entities']['urls'][0]['expanded_url']) == filex[0]:
print(str(tweet['created_at']))
break
It's hard to tell exactly what you're asking, but I suspect you want to loop through the lines in both files in parallel.很难确切地说出你在问什么,但我怀疑你想并行遍历两个文件中的行。
with open("paf.json", "r") as json_file, open("file.txt", "r") as text_file:
for json_line, text_line in zip(json_file, text_file):
tweet = json.loads(json_line)
if tweet['entities']['urls'][0]['expanded_url'] == text_line:
print(tweet['created_at'])
This will tell you if line N in the text file matches the URL in line N in the JSON file.这将告诉您文本文件中的第 N 行是否与 JSON 文件中第 N 行中的 URL 匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.