Advertisement
Guest User

Untitled

a guest
Dec 31st, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import pwb
  4. import pywikibot
  5. import sys
  6. import codecs
  7. import json
  8. import re
  9.  
  10. try:
  11.     f2 = codecs.open('CSV_OSM.txt', 'ab', 'utf8')
  12. except (OSError, IOError) as e:
  13.     print u'Problemes per obrir l\'arxiu %s' % arxiu
  14.     exit(0)
  15. #"ARTICLEVIQUIPEDIA" P402 "NUMIDENTIFCADOROSM" S143 Q936
  16.  
  17. def main():
  18.     try:
  19.         f = codecs.open("JSONOSM.json", 'r', 'utf8')
  20.     except (OSError, IOError) as e:
  21.         print u'Problemes per obrir l\'arxiu %s' % arxiu
  22.         exit(0)
  23.     txt = f.read()
  24.     f.close()
  25.     jsonObject = json.loads(txt)
  26.     for item in jsonObject['elements']:
  27.         identificador = ""
  28.         article = ""
  29.         element = ""
  30.         auxiliar = ""
  31.         linia = ""
  32.         if item['type'] == "relation":
  33.             identificador = item['id']
  34.             if identificador:
  35.                 if item['tags']:
  36.                     try:
  37.                         article = item['tags']['wikipedia']
  38.                     except:
  39.                         element = item['tags']['name']
  40.                         print u"L\'element %s no té el tag Wikipedia" % (element)
  41.                     if article:
  42.                         auxiliar = re.sub(u'(.*?):', '', article)  
  43.                         #auxiliar = re.findall(u'ca\:(.*?)', article)
  44.                         linia = u"%s\tP402\t\"%s\"\tS143\tQ936\n" % (auxiliar, identificador)
  45.                         f2.write(linia)    
  46.                        
  47. if __name__ == '__main__':
  48.     main()
  49.     f2.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement