I'm looping through a XML document and matching usernames from a txt-file.
The txt looks like:
DPL bot
Nick Number
White whirlwind
Polisci
Flannel
And the program looks like:
import xmltodict, json
with open('testarticles.xml', encoding='latin-1') as xml_file:
dic_xml = xmltodict.parse(xml_file.read())
for page in dic_xml['mediawiki']['page']:
for rev in page['revision']:
for user in open("usernames.txt", "r"):
print(user)
if 'username' in rev['contributor'] and rev['contributor']['username'] == user:
print(user)
print(rev['timestamp'])
timestamp = rev['timestamp'];
try:
print(rev['comment'])
comment = rev['comment'];
except:
print("no comment")
comment = ''
print('\n')
with open("User data/" + user + ".json", "a") as outfile:
json.dump({"timestamp": timestamp, "comment": comment}, outfile)
outfile.write('\n')
The problem is that the program only goes through the if-statement for the last line in the text file. It prints all the users' names before the if-statement. All users have matching posts in the XML-file and by changing to another user at the end line, that user's data is extracted into the json file.
elseclause and printrev['contributor']to see what's going on when it fails? Tryif 'username' in rev['contributor'] and rev['contributor']['username'] == user.strip():for line in open(...):automatically assumewithcontext and thus close the file when the loop is done?open()doesn't work like that normally but, then again, it'sopen()plus aforloop which is a rather "ephemeral" construct that doesn't persist beyond it's scope. Also, there's nothing to callfile.close()on sinceopen()has no handle when used in aforloop. It's all speculation though hehe.