【发布时间】:2021-10-06 16:56:38
【问题描述】:
我正在将字典读入保存到 csv 中的 python。我正在阅读这段代码:
import csv
reader = csv.reader(open('out.csv', 'r'))
d = {}
for row in reader:
k, v = row
d[k] = v
得到这个结果...
{'results': '[{\'alternatives\': [{\'transcript\': \'\', \'confidence\': 0.0, \'words\': []}], \'language_code\': \'en-us\', \'channel_tag\': 0}, {\'alternatives\': [{\'transcript\': "okay so okay I\'ll 22nd yes let\'s use my phone cool thank you all right let\'s get the clock up here yeah 20 seconds okay so we\'re going to be calm and relaxed and chill and just sit still for twice I guess no problem all right ready yep", \'confidence\': 0.9024933, \'words\': [{\'start_time\': \'7.700s\', \'end_time\': \'8.200s\', \'word\': \'okay\', \'confidence\': 0.0, \'speaker_tag\': 0}, {\'start_time\': \'8.500s\', \'end_time\': \'9.500s\', \'word\': \'so\', \'confidence\': 0.0, \'speaker_tag\': 0}, {\'start_time\': \'9.600s\', \'end_time\': \'9.800s\', \'word\': \'okay\', \'confidence\': 0.0, \'speaker_tag\': 0}, {\'start_time\': \'9.800s\', \'end_time\': \'9.900s\', \'word\': "I\'ll", \'confidence\': 0.0, \'speaker_tag\': 0}, {\'start_time\': \'9.900s\', \'end_time\': \'10.400s\', \'word\': \'22nd\', \'confidence\': 0.0, \'speaker_tag\': 0}, {\'start_time\': \'10.400s\', \'end_time\': \'10.600s\', \'word\': \'yes\', \'confidence\': 0.0, \'speaker_tag\': 0}
...
出现 \ 字符是怎么回事?当我在 termianl (cat out.csv) 中查看原始文件时,我得到了这个:
results,"[{'alternatives': [{'transcript': '', 'confidence': 0.0, 'words': []}], 'language_code': 'en-us', 'channel_tag': 0}, {'alternatives': [{'transcript': ""okay so okay I'll 22nd yes let's use my phone cool thank you all right let's get the clock up here yeah 20 seconds okay so we're going to be calm and relaxed and chill and just sit still for twice I guess no problem all right ready yep"", 'confidence': 0.9024933, 'words': [{'start_time': '7.700s', 'end_time': '8.200s', 'word': 'okay', 'confidence': 0.0, 'speaker_tag': 0}, {'start_time': '8.500s', 'end_time': '9.500s', 'word': 'so', 'confidence': 0.0, 'speaker_tag': 0}, {'start_time': '9.600s', 'end_time': '9.800s', 'word': 'okay', 'confidence': 0.0, 'speaker_tag': 0}, {'start_time': '9.800s', 'end_time': '9.900s', 'word': ""I'll"", 'confidence': 0.0, 'speaker_tag': 0}, {'start_time': '9.900s', 'end_time': '10.400s', 'word': '22nd', 'confidence': 0.0, 'speaker_tag': 0}, {'start_time': '10.400s', 'end_time': '10.600s', 'word': 'yes', 'confidence': 0.0, 'speaker_tag': 0}, {'start_time': '10.600s', 'end_time': '10.800s', 'word': ""let's"", 'confidence': 0.0, 'speaker_tag': 0}, {'start_time': '10.800s', 'end_time': '11s', 'word': 'use', 'confidence': 0.0, 'speaker_tag': 0}, {'start_time': '11s', 'end_time': '11.100s', 'word': 'my', 'confidence': 0.0, 'speaker_tag': 0}, {'start_time': '11.100s', 'end_time': '11.400s', 'word': 'phone', 'confidence': 0.0, 'speaker_tag': 0}, {'start_time': '11.400s', 'end_time': '11.600s', 'word': 'cool', 'confidence': 0.0, 'speaker_tag': 0}, {'start_time': '11.600s', 'end_time': '11.900s', 'word': 'thank', 'confidence': 0.0, 'speaker_tag': 0}
这似乎是正确的。
我只是希望能够像普通的 python dict 一样通过键来解析它。作为参考,我使用以下命令将 dict 从 google 语音保存到文本 api:
import proto
# response variable comes from normal google speech to text workflow
response_json = proto.Message.to_dict(response)
with open('out.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
for key, value in response_json.items():
writer.writerow([key, value])
【问题讨论】:
-
您不是在读取 CSV 文件。您正在读取一个 JSON 文件。
-
它们是转义字符,用于指示不应分隔字符串结尾的单引号字符文字。
-
我不知道
protoapi,但在您的作者中,value似乎是一个复杂的数据结构。writerow将写入该数据的字符串表示形式。你可以在 writerow 之前print(repr(value))看看那是什么。现在您必须决定如何将数据提取为 CSV 文件的列格式。或者决定使用不同的序列化器,如 json 或 pickle 来保存数据。 -
您的第一个和第二个结果值是相同的 - 只是编码不同。类似于数字 1000 和 10^3 - 它们是相同的数字,只是编码不同。结果的主要项目是用于开始字符串的字符以及用于在该字符串中定义键/值的字符。 \' 是单引号字符的转义,这意味着按字面意思使用它,而不是作为字符串开始或终止字符。
标签: python dictionary google-cloud-platform google-cloud-speech