Advertisement
TShiva

Cinema_searcher

Jul 20th, 2016
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. import json
  2. import xml.etree.ElementTree as ET
  3. def txt_to_xml():
  4.     file_cont = list(open('movies.txt','r',encoding='cp1252').readlines())
  5.     xmlfile = open('xmlfile.xml','w',encoding='cp1252')
  6.     xmlfile.write('<films>\n')
  7.     for film in file_cont:
  8.         film.strip()
  9.         words = film.split('/')
  10.         #film_and_actors['Film']=words.pop(0)
  11.         #film_and_actors['Actors']= words
  12.         xmlfile.write('<film>\n')
  13.         xmlfile.write('<caption>'+words.pop(0)+'</caption>\n')
  14.         for name in words:
  15.             xmlfile.write('<actor>'+words.pop(0)+'</actor>\n')
  16.         xmlfile.write('</film>\n\n')
  17.     xmlfile.write('</films>\n')
  18.     xmlfile.close()
  19.  
  20. def xml_to_json():
  21.     file = list(open('xmlfile.xml', 'r').readlines())
  22.     jsonfile = open('jsonfile.json', 'w')
  23.     for string in file:
  24.         new_string=json.dumps(string)
  25.         jsonfile.write(new_string+"\n")
  26.     jsonfile.close()
  27.  
  28. def two_actors():
  29.     print('Наберите имена 2х актёров,и мы отобразим фильмы,в которых они принимали участие')
  30.     name1=input()
  31.     name2=input()
  32.     actors=[name1,name2]
  33.     return actors
  34.  
  35. def cinema_search():
  36.     filemap = ET.parse('xmlfile.xml')
  37.     actors=two_actors()
  38.     result=[]
  39.     for actor in actors:
  40.         s=set()
  41.         film_node=filemap.findall(".//film/[actor='" + actor + "']" )
  42.         for film in film_node:
  43.             film_name = film.findall(".//caption")[0].text
  44.             s.add(film_name)
  45.         result.append(s)
  46.     print(result)
  47.  
  48. txt_to_xml()
  49. xml_to_json()
  50. cinema_search()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement