我正在阅读一个字典到python保存成csv。我正在阅读这个代码:
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}
...
出现字符是怎么回事?当我在终端(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字典一样通过键来解析它。作为参考,我保存了字典从谷歌语音到文本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格式。在这里,您可以一步一步地看到如何转换为JSON。
这是一个将.csv转换为JSON的示例代码:
import pandas as pd
df = pd.read_csv (r'Path where the CSV file is savedFile Name.csv')
df.to_json (r'Path where the new JSON file will be storedNew File Name.json')
您需要在读取" response_json.items() "的" FOR "语句之前添加此代码
这是一个示例代码: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)
/*
Convert to JSON format
*/
for key, value in response_json.items():
writer.writerow([key, value])