Làm cách nào để xóa một phần tử khỏi đối tượng json trong python?

seanspotatobusinessBài viết. 221Tham gia. Thứ ba ngày 22 tháng 5 năm 2012 11. 19 giờ tối

Tôi đang cố gắng sử dụng một số mã mà tôi đã sao chép và dán vào giữa một tập lệnh để thay đổi cách tập lệnh hoạt động nhưng tôi không thể tìm ra cách hợp lệ để sửa đổi tập lệnh để tập lệnh chạy mà không gặp lỗi. Tôi lấy đoạn mã sau đã được sao chép và dán

Mã.

with requests.request['get', 'api.openweathermap.org/data/2.5/weather', timeout=30, headers=headers] as response: #this line is from my script
        
        with open['response.json', 'r'] as data_file:  #this line is from StackOverflow
            data = json.load[data_file]

        for element in data:
            element.pop['hours', None]

        with open['response.json', 'w'] as data_file:
            data = json.dump[data, data_file]
        
        newData = response.json[] #this line is from my script

và sửa đổi nó thành cái này.

Mã.

with requests.request['get', 'api.openweathermap.org/data/2.5/weather', timeout=30, headers=headers] as response:
        
        data = response.json[]

        for element in data:
            element.pop['pressure', None]
            element.pop['humidity', None]

        newData = data
            


But it won't run, returning error: AttributeError: 'str' object has no attribute 'pop'

Why is data a string? The response from the server that I'm trying to modify is JSON, not a string. Thanks.

Raspberry Pi 3 B và B+
PRETTY_NAME="Raspbian GNU/Linux 10 [buster]"
NAME="Raspbian GNU/Linux"

Chủ Đề