u-urlencode.py 730 B

123456789101112131415161718192021222324
  1. # Generates u-urlencoded.txt from utf-8.txt
  2. #
  3. # u-urlencoded.txt is used by Tests_Formatting_UrlEncodedToEntities
  4. import codecs
  5. import sys
  6. def uurlencode(line):
  7. """Use %u[hexvalue] percent encoding."""
  8. line = line.strip()
  9. line = ["%%u%04X" % ord(s) for s in line]
  10. return "".join(line)
  11. if __name__ == "__main__":
  12. args = sys.argv[1:]
  13. if args and args[0] in ("-h", "--help"):
  14. print "Usage: python u-urlencode.py < utf-8.txt > u-urlencoded.txt"
  15. sys.exit(2)
  16. sys.stdin = codecs.getreader("utf-8")(sys.stdin)
  17. sys.stdout = codecs.getwriter("ascii")(sys.stdout)
  18. lines = sys.stdin.readlines()
  19. sys.stdout.write( "\n".join(map(uurlencode, lines)) )