我正在尝试处理存储在文本文件中的推文。我的代码(一个接一个)读取推文,处理它们,然后将 Watson 的结果保存在 csv 文件中。速度仅为每分钟约 28 条推文。数据文件处理是否会导致此延迟?
while 1:
where = file.tell()
line = file.readline()
if not line:
print "no line found, waiting for a 1 seconds"
time.sleep(1)
file.seek(where)
else:
if (re.search('[a-zA-Z]', line)):
print "-----------------------------"
print "the line is: "
print line
print "-----------------------------"
response = natural_language_understanding.analyze(
text=line,
features=Features(
entities=EntitiesOptions(
emotion=True,
sentiment=True,
limit=2),
keywords=KeywordsOptions(
emotion=True,
sentiment=True,
limit=2)),
language='en'
)
response["tweet"] = line
print(json.dumps(response, indent=2))
with open('#DDvKXIP.csv', 'a') as csv_file:
writer = csv.writer(csv_file)
for key, value in response.items():
writer.writerow([key, value])
else:
print "--------------------------------"
print "found a line without any alphabet in it, hence not considering."
print line
print "--------------------------------"