StringFormat.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. import platform
  5. def FormatStdoutString(content):
  6. """
  7. 代码中写的string默认设置成utf-8
  8. 而调试输出可能编码不一样,则需要转换
  9. """
  10. if sys.stdout.encoding == 'cp936':
  11. content = content.decode('utf-8').encode('cp936')
  12. elif sys.stdout.encoding == 'gb2312':
  13. content = content.decode('utf-8').encode('cp936')
  14. elif sys.stdout.encoding == 'utf-8':
  15. content = content
  16. return content
  17. def SplitSpaceInString(content):
  18. """
  19. 剔除字符串中所有空白字符
  20. """
  21. return ''.join(content.split())
  22. def FormatStringPlatform(content):
  23. platformName = platform.system()
  24. if platformName == 'Windows':
  25. return content.decode('utf-8').encode('cp936')
  26. else:
  27. return content
  28. def FormatStdinString(content):
  29. if sys.stdout.encoding == 'cp936':
  30. content = content.decode('cp936').encode('utf-8')
  31. elif sys.stdout.encoding == 'gb2312':
  32. content = content.decode('cp936').encode('utf-8')
  33. elif sys.stdout.encoding == 'utf-8':
  34. content = content
  35. return content