我的数据采用以下格式:
[{u'value': 7681, u'time': u'2017-07-12T12:15:54.107488923Z'}, {u'value':
7672, u'time': u'2017-07-12T12:26:01.295268409Z'}]
我需要从此数据中删除所有 u
前缀。我怎么能用Python 2.7做到这一点?事实上,我希望它像:
[{'value': 7681, 'time': '2017-07-12T12:15:54.107488923Z'}, {'value':
7672, 'time': '2017-07-12T12:26:01.295268409Z'}]
答案 0 :(得分:1)
目前还不清楚ResultSet
是什么以及问题中的格式,但以下示例代码可能会有所帮助:
import csv
csv_filename = 'result_set.csv'
ResultSet = {"(u'maxbotix_depth', None)": [{u'time': u'2017-07-12T12:15:54.107488923Z',
u'value': 7681},
{u'time': u'2017-07-12T12:26:01.295268409Z',
u'value': 7672}]}
with open(csv_filename, mode='wb') as csv_file:
writer = csv.writer(csv_file)
for obj in ResultSet["(u'maxbotix_depth', None)"]:
time, value = obj[u'time'], obj[u'value']
print('time: {}, value: {}'.format(time, value)) # optional
writer.writerow((time, value))
印刷输出:
time: 2017-07-12T12:15:54.107488923Z, value: 7681
time: 2017-07-12T12:26:01.295268409Z, value: 7672
创建的文件内容:
2017-07-12T12:15:54.107488923Z,7681
2017-07-12T12:26:01.295268409Z,7672