convert.py 918 B

123456789101112131415161718192021222324
  1. def convertString(input_string, encoding):
  2. with open('codemap.txt', 'r', encoding='utf-8') as f:
  3. for line in f:
  4. chinese, utf8, big5 = line.split()
  5. if chinese == input_string:
  6. if encoding == 'utf-8':
  7. return int(utf8)
  8. else:
  9. return int(big5)
  10. return -1
  11. inputString = input("輸入你要轉換的中文句子:")
  12. encoding = input("選擇輸出編碼:(utf-8 或 big5): ")
  13. if encoding not in ['utf-8', 'big5']:
  14. print("不支援所選編碼!")
  15. else:
  16. print("請把以下輸出複製到 Arduino 的按鍵輸出功能下:\n")
  17. for char in inputString:
  18. zhCharCode = convertString(char,encoding)
  19. if (zhCharCode is -1):
  20. print("Keyboard_write('" + char + "');")
  21. else:
  22. print('typeChinese("' + str(zhCharCode) + '"); //' + char)