Appending JSON data from one file to another in Python

Mr. Concolato

Consider this code, which is understandably failing:

def testDataTransform():
    source = 'data/threads/testFile2.json' 
    newFile = 'data/threads/testFile1.json'

    jX = returnJson(source)
    jY = returnJson(newFile)

    for dataL1 in jX:
        #print dataL1['city']
        for dataL2 in jY:
            if dataL1['city'] == dataL2['city']:
                dataL2.append(dataL1['population']) 

    print dataL2


# end testDataTransform

def returnJson(source):
    #Grab destination file json
    try:
       with open(source) as jsonFile: # Open and verify file is there
        # load JSON object into memory
        j = json.load(jsonFile)

        return j
    except Exception, e:
        print e
        raise
# end returnJson

testDataTransform()

The error generated is: AttributeError: 'dict' object has no attribute 'append', which I now understand is due to trying to use append on a file steam and thus appending my JSON incorrectly. The question is how would I do this correctly? I want to add population data, which is in the source file.

JSON structure in testFiles look like this:

[

    {
        "city": "New York",
        "lat": 20.1234,
        "long": 32.09876
    },
    {
        "city": "London",
        "lat": 21.1234,
        "long": 37.09876
    },
    {
        "city": "New Jersey",
        "lat": 10.1234,
        "long": 30.09876
    }

]

One has a "population" node and other does not. I want to copy that population data from one file to another. How is that done? I wonder if converting the file stream into an array would help, but how do I even do that?

Thanks.

kdopen

The error message is telling you exactly what's wrong. It's in this line

dataL2.append(dataL1['population']) 

dataL2 is a dict(), and dict() does not have a method called append

What you want is

dataL2["population"] = dataL1['population']

Basically, the call to json.load() for this file returns a list of dicts(). JSON arrays map to Python lists, JSON objects map to Python dictionaries. To add a new key to a dict, simply set a value on it.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Python: Appending one text array to another adds a "u"?

분류에서Dev

Navigate from one php file to another carrying array of data

분류에서Dev

Reading sentences from a text file and appending into a list with Python 3

분류에서Dev

appending a list from a read text file python3

분류에서Dev

Passing php variable from one file to another?

분류에서Dev

Notifying data change from one activity to another

분류에서Dev

Using data from one ajax request in another

분류에서Dev

Send data from one DialogFragment to another in Android

분류에서Dev

Copy data from one column to another column

분류에서Dev

Print data from json file

분류에서Dev

Appending item to dictionary from another dictionary

분류에서Dev

Appending rows returned from different queries into one

분류에서Dev

Moving elements from one xml file to Another xml file

분류에서Dev

Appending file name to data frame in R

분류에서Dev

Send values from one PHP file to another with JavaScript/AJAX

분류에서Dev

Undefined Method - Calling a class in one file from another

분류에서Dev

JavaScript: Add JSON objects from one array to another array conditionally

분류에서Dev

Proc sql: Creating a view by copying data from one year to another

분류에서Dev

best way to select data from one table or another

분류에서Dev

Data transfer from one pc to another using pulse coded modulation

분류에서Dev

Moving some data from one column of MySQL table to another

분류에서Dev

Insert data from a table to another one in MySQL with where condition

분류에서Dev

SQL server : Inserting/updating missing data from one table to another

분류에서Dev

Swift watch. Sending data from one view controller to another

분류에서Dev

Get nested data from local JSON file

분류에서Dev

Accessing data from Wikidata JSON file

분류에서Dev

How to get data from JSON file?

분류에서Dev

Get Data from JSON file into jQuery

분류에서Dev

How to fetch data from another excel file and sort it under name?

Related 관련 기사

  1. 1

    Python: Appending one text array to another adds a "u"?

  2. 2

    Navigate from one php file to another carrying array of data

  3. 3

    Reading sentences from a text file and appending into a list with Python 3

  4. 4

    appending a list from a read text file python3

  5. 5

    Passing php variable from one file to another?

  6. 6

    Notifying data change from one activity to another

  7. 7

    Using data from one ajax request in another

  8. 8

    Send data from one DialogFragment to another in Android

  9. 9

    Copy data from one column to another column

  10. 10

    Print data from json file

  11. 11

    Appending item to dictionary from another dictionary

  12. 12

    Appending rows returned from different queries into one

  13. 13

    Moving elements from one xml file to Another xml file

  14. 14

    Appending file name to data frame in R

  15. 15

    Send values from one PHP file to another with JavaScript/AJAX

  16. 16

    Undefined Method - Calling a class in one file from another

  17. 17

    JavaScript: Add JSON objects from one array to another array conditionally

  18. 18

    Proc sql: Creating a view by copying data from one year to another

  19. 19

    best way to select data from one table or another

  20. 20

    Data transfer from one pc to another using pulse coded modulation

  21. 21

    Moving some data from one column of MySQL table to another

  22. 22

    Insert data from a table to another one in MySQL with where condition

  23. 23

    SQL server : Inserting/updating missing data from one table to another

  24. 24

    Swift watch. Sending data from one view controller to another

  25. 25

    Get nested data from local JSON file

  26. 26

    Accessing data from Wikidata JSON file

  27. 27

    How to get data from JSON file?

  28. 28

    Get Data from JSON file into jQuery

  29. 29

    How to fetch data from another excel file and sort it under name?

뜨겁다태그

보관