Skip to content

Instantly share code, notes, and snippets.

@eurycea
Created May 6, 2015 16:41
Show Gist options
  • Save eurycea/ff25f9e85c6755dff921 to your computer and use it in GitHub Desktop.
Save eurycea/ff25f9e85c6755dff921 to your computer and use it in GitHub Desktop.
strip xml from android strings.xml for a simple human readable output
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