我正在尝试将我的 CSV 文件转换为 JSON 文件格式。当我这样做时,JSON 文件中有一个额外的条目,它只包含字段名称。
我尝试过使用熊猫、字典,但似乎无法得到结果。某事或其他事来了。
我想在 JSON 开头删除额外的文件名条目。另外,我怎样才能使 ConnectionId 作为键并为不同的输出设置相同的格式。
import csv, json
csvfile = open('/home/Desktop/PD/GEOSubscriberLocations_LTE_sample.csv', 'r')
jsonfile = open('/home/Desktop/PD/script5.json', 'w')
fieldnames = ("Confidence", "ConnectionId", "Imei", "Imsi", "IsData", "IsSignalling", "IsVoice", "Latitude", "Longitude",
"Mcc", "Mnc", "SegmentDuration", "SegmentStartTime", "ServingCellLabel", "Sv",
"TrackingAreaCode", "Uncertainity")
reader = csv.DictReader(csvfile , fieldnames)
code = ''
for row in reader:
for key in row:
row[key] = row[key].decode('utf-8', 'ignore').encode('utf-8')
json.dump(row, jsonfile, indent=4, sort_keys=False)
jsonfile.write('\n')
如果使用 ConnectionId 作为键,我希望我的输出如下:
{
"ConnectionId": "189970698469977",
{
"Confidence": "0.01428183",
"Imei": "99999507405260",
"Imsi": "999992226504812",
"IsData": "FALSE",
"IsSignalling": "TRUE",
"IsVoice": "FALSE",
"Latitude": "1.848613",
"Longitude": "1.354355",
"Mcc": "999",
"Mnc": "99",
"SegmentDuration": "00:00:00.0860000",
"SegmentStartTime": "16/02/2017 09:57:00.053",
"ServingCellLabel": "Cell14",
"Sv": "06",
"TrackingAreaCode": "1256",
"Uncertainty": 662
}
隔江千里
慕莱坞森
相关分类