【问题标题】:Change key value of dictionary using python使用python更改字典的键值
【发布时间】: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


【解决方案1】:

从代码来看,json_doc 看起来像一本字典。我们可以使用

dictionary[new_key] = dictionary.pop(old_key)

在打印结果之前做一些处理:

json_doc = {'Object': '108', 'Easting': 50.15466792673487, 'Northing': -5.066927538249519}
json_doc['Latitude'] = json_doc.pop('Easting')
json_doc['Longitude'] = json_doc.pop('Northing')
print(json_doc)

输出

{'Object': '108', 'Latitude': 50.15466792673487, 'Longitude': -5.066927538249519}

【讨论】:

    【解决方案2】:

    写旧键的拷贝和删除

    json_doc['Latitude'] = json_doc['Easting']
    del json_doc['Easting'] 
    json_doc['Longitude'] = json_doc['Northing']
    del json_doc['Northing']
    

    【讨论】:

    • 我试过了,结果keys ['Object', 'Easting', 'Northing'] 'Easting'
    • @fullaquestions 将我的代码放在for idx,el in enumerate(row) 循环之后,打印print(json_doc) 时您应该会看到它
    猜你喜欢
    • 1970-01-01
    • 2018-12-05
    • 2022-07-12
    • 1970-01-01
    • 2018-06-20
    • 2017-02-03
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    相关资源
    最近更新 更多