import sys from bs4 import BeautifulSoup def characters(soup): for character in soup.find_all('characters'): c_elems = character.find_all('c') if len(c_elems) > 0: print('Found some appropriate c-tags') for char_c in c_elems: # print(char_c['name']) if 'name' in char_c.attrs: print(char_c['name']) # We have found a character tag! # ---- SKILL UPRGRADING skill_tag = char_c.find('skills') print(skill_tag) # Experimental changing for sk_tag in skill_tag.find_all('s'): sk_tag['level'] = '5' sk_tag['mxn'] = '8' if 'mxp' in sk_tag.attrs: sk_tag['mxp'] = '8' print(skill_tag) # ---- ATTRIBUTE UPGRADING attribute_tag = char_c.find('attr') print(attribute_tag) for a_tag in attribute_tag.find_all('a'): a_tag['points'] = '6' print(attribute_tag) def inventory(soup): # Load tag names first: id_dict = {} filename = "Reduced Ids 2.txt" for line in open(filename): result = line.split() code = int(result[0]) name = ' '.join(result[1:]) # print(code, name) id_dict[code] = name # print(id_dict) item_tracking = {} for inv_tag in soup.find_all('inv'): if inv_tag.parent.name != 'feat': continue quantity_total = 0 for s_tag in inv_tag.find_all('s'): item_code = int(s_tag['elementaryId']) item_quantity = int(s_tag['inStorage']) item_name = id_dict[item_code] print("{}: {}, quantity: {}".format(item_code, item_name, item_quantity)) quantity_total += item_quantity if item_code not in item_tracking: item_tracking[item_code] = item_quantity else: item_tracking[item_code] = item_tracking[item_code] + item_quantity print('Total use of this storage space: {}'.format(quantity_total)) print('-----') print('Item total summary:') for item in item_tracking.items(): item_code = item[0] item_name = id_dict[item_code] item_quantity = item[1] print('{:04} - {} - {}'.format(item_code, item_name, item_quantity)) def main(): filename = sys.argv[1] print(filename) full_text = "" for line in open(filename): full_text += line # print(full_text) soup = BeautifulSoup(full_text, "xml") # print(soup.prettify()) # characters(soup) inventory(soup) text = soup.prettify() # Delete XML header sansfirstline = '\n'.join(text.split('\n')[1:]) f = open('game.xml', 'w') f.write(sansfirstline) f.close() if __name__ == "__main__": main()