【发布时间】:2019-04-22 08:07:12
【问题描述】:
我正在尝试将我的 CSV 文件转换为 JSON 文件格式。当我这样做时,JSON 文件中有一个额外的条目,它只包含字段名称。
我尝试过使用 pandas、字典,但似乎无法得到结果。某事或其他事来了。
我想删除 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')
实际结果是:
{
"Confidence": "Confidence",
"IsData": "IsData",
"Latitude": "Latitude",
"ConnectionId": "ConnectionId",
"Mcc": "Mcc",
"Sv": "Sv",
"Longitude": "Longitude",
"Uncertainity": "Uncertainty",
"IsVoice": "IsVoice",
"IsSignalling": "IsSignalling",
"SegmentStartTime": "SegmentStartTime",
"Imei": "Imei",
"SegmentDuration": "SegmentDuration",
"Mnc": "Mnc",
"ServingCellLabel": "ServingCellLabel",
"Imsi": "Imsi",
"TrackingAreaCode": "TrackingAreaCode"
}
{
"Confidence": "1.994667E-07",
"IsData": "FALSE",
"Latitude": "1.694202",
"ConnectionId": "330708186825281",
"Mcc": "999",
"Sv": "01",
"Longitude": "0.434623",
"Uncertainity": "178",
"IsVoice": "FALSE",
"IsSignalling": "TRUE",
"SegmentStartTime": "16/02/2017 09:56:59.912",
"Imei": "99999006686069",
"SegmentDuration": "00:00:00.0350000",
"Mnc": "99",
"ServingCellLabel": "Cell18",
"Imsi": "999992223223602",
"TrackingAreaCode": "1234"
}
{
"Confidence": "1.504506E-12",
"IsData": "FALSE",
"Latitude": "1.633704",
"ConnectionId": "260339442647675",
"Mcc": "999",
"Sv": "02",
"Longitude": "0.668554",
"Uncertainity": "314",
"IsVoice": "FALSE",
"IsSignalling": "TRUE",
"SegmentStartTime": "16/02/2017 09:57:01.377",
"Imei": "99999207564306",
"SegmentDuration": "00:00:00.0280000",
"Mnc": "99",
"ServingCellLabel": "Cell19",
"Imsi": "999993793410366",
"TrackingAreaCode": "1235"
}
{
"Confidence": "0.3303348",
"IsData": "FALSE",
"Latitude": "1.847635",
"ConnectionId": "260339442647676",
"Mcc": "999",
"Sv": "14",
"Longitude": "1.356349",
"Uncertainity": "129",
"IsVoice": "FALSE",
"IsSignalling": "TRUE",
"SegmentStartTime": "16/02/2017 09:57:01.555",
"Imei": "99999605176135",
"SegmentDuration": "00:00:00.0290000",
"Mnc": "99",
"ServingCellLabel": "Cell13",
"Imsi": "999992216631694",
"TrackingAreaCode": "1236"
}
{
"Confidence": "0.01800376",
"IsData": "FALSE",
"Latitude": "1.914598",
"ConnectionId": "330708186825331",
"Mcc": "999",
"Sv": "74",
"Longitude": "1.222736",
"Uncertainity": "463",
"IsVoice": "FALSE",
"IsSignalling": "TRUE",
"SegmentStartTime": "16/02/2017 09:57:02.689",
"Imei": "99999007880884",
"SegmentDuration": "00:00:00.0260000",
"Mnc": "99",
"ServingCellLabel": "Cell7",
"Imsi": "999992226681236",
"TrackingAreaCode": "1237"
}
{
"Confidence": "0.2068138",
"IsData": "FALSE",
"Latitude": "1.850279",
"ConnectionId": "330708186825354",
"Mcc": "999",
"Sv": "13",
"Longitude": "1.349263",
"Uncertainity": "167",
"IsVoice": "FALSE",
"IsSignalling": "TRUE",
"SegmentStartTime": "16/02/2017 09:57:04.351",
"Imei": "99999002855874",
"SegmentDuration": "00:00:00.0300000",
"Mnc": "99",
"ServingCellLabel": "Cell15",
"Imsi": "999995430231562",
"TrackingAreaCode": "1238"
}
如果使用 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
}
【问题讨论】: