Created
May 6, 2015 16:41
-
-
Save eurycea/ff25f9e85c6755dff921 to your computer and use it in GitHub Desktop.
strip xml from android strings.xml for a simple human readable output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ET | |
def parse_text(line): | |
clean_line = line | |
clean_line = clean_line.replace("…", "...") | |
clean_line = clean_line.replace("\u2020", "") | |
clean_line = clean_line.replace("–", "-") | |
clean_line = clean_line.replace("©", "(copy)") | |
element = ET.fromstring(clean_line) | |
return element.text | |
def parse_comment(line): | |
return line.strip()[4:-3].strip() | |
def is_comment(line): | |
return "<!--" in line | |
def is_string_element(line): | |
return "<string name=" in line | |
if __name__ == "__main__": | |
with open("app/src/main/res/values/strings.xml") as f: | |
lines = [line for line in f.readlines() if line] | |
with open("human_strings_output.txt", "w+") as output: | |
for line in lines: | |
if is_comment(line): | |
if not is_string_element(line): | |
output.write("\n"+parse_comment(line) + ":\n")#preserve comments used to group strings | |
elif is_string_element(line): | |
try: | |
output.write("\t"+parse_text(line) + "\n")#nest under previous group/category comment | |
except Exception, e: | |
print(e) | |
print(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment