Python3:如何将字符串转换为“ \ x00 ....”

时间:2018-08-18 10:29:27

标签: python python-3.x

如何将字符串"Серия 1"转换为字符串"\x412\x437\x440\x44b\x432\x430\x44f"以写入文件。

def create_playlist(playlist):
  gplaylist = "[playlist]\n"
  playlist1 = json.loads(playlist)
  x = 1;
  for i in enumerate(playlist1):
    for j in enumerate(i[1]['folder']):
      gplaylist += "File" + str(x) + "=" + parse_file(j[1]['file']) + "\n"
      # Variable: j[1]['title'] must converted to "\x412\x437\x440\x44b\x432\x430\x44f"
      gplaylist += "Title" + str(x) + "=" + j[1]['title'] + "\n"
      x += 1
  gplaylist += "NumberOfEntries=" + str(x-1)
  write_playlist(gplaylist)

def write_playlist(playlist):
   with io.open('play_list.pls', 'w', encoding='utf-8') as outfile:
      outfile.write(to_unicode(playlist))

1 个答案:

答案 0 :(得分:0)

您应该在没有真正必要的地方停止使用编码。一切都按原样完美运行:

$ python
>>> with open('part1.txt', 'w') as fout :
...     fout.write( 'Серия 1\n' )
... 
>>> 
$ cat part1.txt
Серия 1
$