import json import urllib.request import urllib.parse def kysi(knimi): aadress="https://api.opencagedata.com/geocode/v1/json?q="+urllib.parse.quote(knimi)+"&key=9ef918759b2d4e259a4c91547440fc41&language=en&pretty=1" sisu=urllib.request.urlopen(aadress).read().decode("utf-8") vastus=json.loads(sisu) if len(vastus["results"])>0: asukoht=vastus["results"][0]["geometry"] riik=vastus["components"]["ISO_3166-1_alpha-2"] print(knimi, asukoht) # f2=open("kohavastused.txt", "a", encoding="utf-8") return str(asukoht["lat"])+","+str(asukoht["lng"])+","+riik # f2.close() else: return "" for rida in open("v1lisa.txt", encoding="utf-8"): m=rida.strip().split() v="" if len(m)<5 and len(m)>2: v=kysi(m[2]) f2=open("v1lisa_vasted.txt", "a", encoding="utf-8") print(rida.strip()+","+v) f2.close()