spark dev
spark dev

Reputation: 35

Passing values from list to dict.get() python

I have a list with 3 values. I want my loop to be able to loop through each list values and be used in dict.get() , instead it is just outputting the same values for every value in the list. I understand it is within my for loop this is happening , but instead of using the same values for every item in the list (using current and previous values from a.json to be used for b.json and c.json ) i would want the items to use their own corresponding values. I have added my code as follows :

def readFileIntoDict(pathOfFile):
    fo = open(pathOfFile, "rw+")
    linesOfFiles = fo.readlines()
    dataInFile = {}
    for line in linesOfFiles:
        jsonD = json.loads(line)
        dataInFile[jsonD['File Name']] = jsonD['File Size']
    return dataInFile

jsonDataprevFile = readFileIntoDict('dates/2018-01-01.json')
jsonDatacurrFile = readFileIntoDict('dates/2018-01-02.json')
list1 = jsonDataprevFile.keys()
list2 = jsonDatacurrFile.keys()
names_in_both = set(list1).intersection(list2)

# At this point it loops through 3 times 
file_names = ['a.json', 'b.json', 'c.json']


for fileNames in file_names:
    if fileNames in names_in_both:

         # Get the key a.json from file_name
         print(compare(jsonDataprevFile.get(file_names[0]), jsonDatacurrFile.get(file_names[0])))

Upvotes: 0

Views: 489

Answers (2)

buran
buran

Reputation: 14233

If I get right what you want to do and assuming compare() is defined elsewhere in the code

for file_name in file_names:
    if file_name in names_in_both:
        # Get the key file_name from both json objects
        print(compare(jsonDataprevFile.get(file_name), jsonDatacurrFile.get(file_name)))

Also, note that your readFileIntoDict() function looks a bit odd - if input json files are indeed valid json, you should not read/parse line by line. Can you upload sample input json file?

Upvotes: 1

Sergey Pugach
Sergey Pugach

Reputation: 5669

You are iterating on file_names but doesn't change the value: Just change file_names[0] to file_name

for file_name in file_names:
    if file_name in names_in_both:

         # Get the key a.json from file_name
         print(compare(jsonDataprevFile.get(file_name), jsonDatacurrFile.get(file_name)))

Upvotes: 1

Related Questions