Skip to content

Instantly share code, notes, and snippets.

@KimMilim
Last active February 5, 2020 00:28
Show Gist options
  • Save KimMilim/269c24436f507b27553d701430f8e50b to your computer and use it in GitHub Desktop.
Save KimMilim/269c24436f507b27553d701430f8e50b to your computer and use it in GitHub Desktop.
Json_parse_week_4.md

Json parse - practice

I'm going to practice on parsing(Json format) Git commit message based on Python. I'm going to read the data from .csv file and get url. And I will save the message that I parsed from url to .csv file.

I need to import Json, csv


import csv
import json

Open file to read and write.


read_f = open('read_file.csv', 'r', encoding = 'utf-8')
write_f = open('write_file.csv', 'w' , encoding = 'utf-8')

rdr = csv.reader(read_f)
wdr = csv.writer(write_f)

add = "https://api.github.com/repos/"

linenum = 0
for line in rdr:
    if linenum != 0:    
        repo = line[1][19:-4]         
        fix = line[4]
        url = add + repo + "/commits/" + fix
        
        my_file = requests.get(url, auth=('KimMilim','kmlsjm98*'))
        try:
            my_dict =json.loads(my_file.text)['commit']['message']
            #print(my_dict)
            wdr.writerow([my_dict])
        except:
            wdr.writerow(["-"])      
    linenum += 1
       
read_f.close()    
write_f.close()  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment