Hallo zusammen,
ich habe ein script zusammengebastelt und nfo-dateien aus kodi auszulesen.
das funktioniert auch soweit prima, nur wenn mal eine der abgefragten infos nicht vorhanden ist gibt es einen fehler und das script bircht ab.
hier das script
Spoiler anzeigen
#!/usr/bin/python
# coding: utf-8
import io, os, sys
import xml.etree.ElementTree as ElementTree
for filename in sys.argv[1:]:
if os.path.exists(filename):
doc = ElementTree.parse(filename)
title = doc.find('title').text or "Kein Titel"
year = doc.find('year').text or "Jahr ?"
plot = doc.find('plot').text or "Keine Inhaltsangabe"
votes = doc.find('ratings/rating/votes').text or "-"
director = doc.find('director').text or "?"
runtime = doc.find('runtime').text or "X"
if runtime == "0": runtime = "X"
rating = doc.find('ratings/rating/value').text
if rating is None: votes = "-"
if rating is None: rating = "-.-"
country = doc.find('country').text or "Land ?"
if country == "United Kingdom": country = "GB"
if country == "France": country = "Frankreich"
if country == "Australia": country = "Australien"
if country == "Belgium": country = "Belgien"
if country == "Germany": country = "Deutschland"
if country == "United States of America": country = "USA"
if country == "Netherlands": country = "Niederlande"
if country == "Sweden": country = "Schweden"
if country == "Canada": country = "Kanada"
if country == "Colombia": country = "Kolumbien"
if country == "Spain": country = "Spanien"
if country == "Argentina": country = "Argentinien"
if country == "Italy": country = "Italien"
if country == "Cuba": country = "Kuba"
if country == "Denmark": country = "D nemark"
if country == "Czech Republic": country = "Tscheschien"
if country == "New Zealand": country = "Neuseeland"
if country == "Russia": country = "Russland"
if country == "Iceland": country = "Island"
names = [n.text or "?" for n in doc.findall('actor/name')]
genre = [g.text or "Genre ?" for g in doc.findall('genre')]
items = [ title, country + ' ' + year + ' (R/V: ' + rating[:3] + '/ ' + votes + ')', runtime + ' min, ' + ', '.join(genre), plot, 'Regie: ' + director, 'Darsteller: ' + ', '.join(names)]
basename = os.path.splitext(filename)[0]
with io.open(basename + '.txt', 'w', encoding='utf-8') as fh:
fh.write(u'\n\n'.join(items))
hier die fehlermeldung
Spoiler anzeigen
# ./xmbc2emc.sh
Traceback (most recent call last):
File "xml2txt.py", line 23, in <module>
country = doc.find('country').text or "Land ?"
AttributeError: 'NoneType' object has no attribute 'text'
in der regel sind alle abgefragten infos in den .nfo dateien vorhanden und alles rennt durch. wie bekommen ich das script dazu eine 'leere' Abfrage zu ignorieren, oder besser noch mit "no info" zu füllen ?
ich habe ein script zusammengebastelt und nfo-dateien aus kodi auszulesen.
das funktioniert auch soweit prima, nur wenn mal eine der abgefragten infos nicht vorhanden ist gibt es einen fehler und das script bircht ab.
hier das script
#!/usr/bin/python
# coding: utf-8
import io, os, sys
import xml.etree.ElementTree as ElementTree
for filename in sys.argv[1:]:
if os.path.exists(filename):
doc = ElementTree.parse(filename)
title = doc.find('title').text or "Kein Titel"
year = doc.find('year').text or "Jahr ?"
plot = doc.find('plot').text or "Keine Inhaltsangabe"
votes = doc.find('ratings/rating/votes').text or "-"
director = doc.find('director').text or "?"
runtime = doc.find('runtime').text or "X"
if runtime == "0": runtime = "X"
rating = doc.find('ratings/rating/value').text
if rating is None: votes = "-"
if rating is None: rating = "-.-"
country = doc.find('country').text or "Land ?"
if country == "United Kingdom": country = "GB"
if country == "France": country = "Frankreich"
if country == "Australia": country = "Australien"
if country == "Belgium": country = "Belgien"
if country == "Germany": country = "Deutschland"
if country == "United States of America": country = "USA"
if country == "Netherlands": country = "Niederlande"
if country == "Sweden": country = "Schweden"
if country == "Canada": country = "Kanada"
if country == "Colombia": country = "Kolumbien"
if country == "Spain": country = "Spanien"
if country == "Argentina": country = "Argentinien"
if country == "Italy": country = "Italien"
if country == "Cuba": country = "Kuba"
if country == "Denmark": country = "D nemark"
if country == "Czech Republic": country = "Tscheschien"
if country == "New Zealand": country = "Neuseeland"
if country == "Russia": country = "Russland"
if country == "Iceland": country = "Island"
names = [n.text or "?" for n in doc.findall('actor/name')]
genre = [g.text or "Genre ?" for g in doc.findall('genre')]
items = [ title, country + ' ' + year + ' (R/V: ' + rating[:3] + '/ ' + votes + ')', runtime + ' min, ' + ', '.join(genre), plot, 'Regie: ' + director, 'Darsteller: ' + ', '.join(names)]
basename = os.path.splitext(filename)[0]
with io.open(basename + '.txt', 'w', encoding='utf-8') as fh:
fh.write(u'\n\n'.join(items))
hier die fehlermeldung
# ./xmbc2emc.sh
Traceback (most recent call last):
File "xml2txt.py", line 23, in <module>
country = doc.find('country').text or "Land ?"
AttributeError: 'NoneType' object has no attribute 'text'
in der regel sind alle abgefragten infos in den .nfo dateien vorhanden und alles rennt durch. wie bekommen ich das script dazu eine 'leere' Abfrage zu ignorieren, oder besser noch mit "no info" zu füllen ?
Gruß wsxws