【发布时间】:2021-06-01 20:50:43
【问题描述】:
我有 python 代码来解析一个 CSV,如下所示
def process_incoming_file(self, bucket, key, event):
try:
line_count = 0
timestamp = 0
print('processing data')
response = self.client.get_object(Bucket=bucket, Key=key)
decoded_content = response['Body'].read().decode('utf-8')
print(decoded_content)
reader = csv.reader(decoded_content.splitlines(), delimiter=',')
rows = list(reader)
for row in rows:
fid = uuid.uuid4()
if line_count == 0:
keys = row
print('keys', keys)
line_count += 1
else:
inProj = Proj(init='epsg:27700') #British National Grid
outProj = Proj(init='epsg:4326') #WGS84
x1, y1 = row[1], row[2] # easting, northing
x2, y2 = transform(inProj, outProj, x1, y1)
row[1], row[2] = y2, x2
json_doc = {}
for idx,el in enumerate(row):
if keys[idx].lower() in ['time', 'date serviced']:
timestamp = self.format_timestring(row[idx])
else:
json_doc[keys[idx]] = row[idx]
print(row[0])
print (fid)
print(timestamp)
print(json_doc)
line_count += 1
except Exception as e:
print(e)
return
上面的代码然后将我的结果返回为:
108
df44d8ef-1ace-40cb-9cb6-ebbce0bb78ed
1579219200
{'Object': '108', 'Easting': 50.15466792673487, 'Northing': -5.066927538249519}
但是,我想将键名从“Easting”更新为“Latitude”,将“Northing”更新为“Longitude”,如下所示:
108
df44d8ef-1ace-40cb-9cb6-ebbce0bb78ed
1579219200
{'Object': '108', 'Latitude': 50.15466792673487, 'Longitude': -5.066927538249519}
有人知道怎么做吗?
【问题讨论】:
-
输出会是什么样子?
-
108 df44d8ef-1ace-40cb-9cb6-ebbce0bb78ed 1579219200 {'Object': '108', 'Latitude': 50.15466792673487, 'Longitude': -5.066927538249519}
标签: python csv dictionary