Board logo

标题: [问题求助] Python字符串转换列表后编码方式不对 [打印本页]

作者: netdzb    时间: 2020-4-4 12:51     标题: Python字符串转换列表后编码方式不对

songtrim = '''PERFORMER "Various"
TITLE "宝丽金非常老牌精选"
FILE "CDImage.ape" WAVE
  TRACK 01 AUDIO
    TITLE "太阳一样"
    PERFORMER "刘文正"
    INDEX 01 00:00:00
  TRACK 02 AUDIO
    TITLE "堆积情感"
    PERFORMER "邝美云"
    INDEX 00 03:28:07
    INDEX 01 03:30:10
  TRACK 03 AUDIO
    TITLE "时空寄情"
    PERFORMER "黄莺莺"
    INDEX 00 08:24:00
    INDEX 01 08:27:12
  TRACK 04 AUDIO
    TITLE "再见我的爱人"
    PERFORMER "邓丽君"
    INDEX 00 12:33:10
    INDEX 01 12:36:32
  TRACK 05 AUDIO
    TITLE "海角天涯"
    PERFORMER "王芷蕾"
    INDEX 00 16:03:02
    INDEX 01 16:06:07
  TRACK 06 AUDIO
    TITLE "廻"
    PERFORMER "李恕权"
    INDEX 00 19:30:30
    INDEX 01 19:33:22
  TRACK 07 AUDIO
    TITLE "故事的真相"
    PERFORMER "黄仲昆、杨林"
    INDEX 00 24:04:05
    INDEX 01 24:06:12
  TRACK 08 AUDIO
    TITLE "扬帆"
    PERFORMER "阳帆"
    INDEX 00 28:34:42
    INDEX 01 28:37:22'''
songname = songtrim.split('\r')
print(songname)
作者: netdzb    时间: 2020-4-4 12:54

回复 1# netdzb

输出的是这个结果。

['PERFORMER "Various"\nTITLE "\xe5\xae\x9d\xe4\xb8\xbd\xe9\x87\x91\xe9\x9d\x9e\xe5\xb8\xb8\xe8\x80\x81\xe7\x89\x8c\xe7\xb2\xbe\xe9\x80\x89"\nFILE "CDImage.ape" WAVE\n  TRACK 01 AUDIO\n    TITLE "\xe5\xa4\xaa\xe9\x98\xb3\xe4\xb8\x80\xe6\xa0\xb7"\n    PERFORMER "\xe5\x88\x98\xe6\x96\x87\xe6\xad\xa3"\n    INDEX 01 00:00:00\n  TRACK 02 AUDIO\n    TITLE "\xe5\xa0\x86\xe7\xa7\xaf\xe6\x83\x85\xe6\x84\x9f"\n    PERFORMER "\xe9\x82\x9d\xe7\xbe\x8e\xe4\xba\x91"\n    INDEX 00 03:28:07\n    INDEX 01 03:30:10\n  TRACK 03 AUDIO\n    TITLE "\xe6\x97\xb6\xe7\xa9\xba\xe5\xaf\x84\xe6\x83\x85"\n    PERFORMER "\xe9\xbb\x84\xe8\x8e\xba\xe8\x8e\xba"\n    INDEX 00 08:24:00\n    INDEX 01 08:27:12\n  TRACK 04 AUDIO\n    TITLE "\xe5\x86\x8d\xe8\xa7\x81\xe6\x88\x91\xe7\x9a\x84\xe7\x88\xb1\xe4\xba\xba"\n    PERFORMER "\xe9\x82\x93\xe4\xb8\xbd\xe5\x90\x9b"\n    INDEX 00 12:33:10\n    INDEX 01 12:36:32\n  TRACK 05 AUDIO\n    TITLE "\xe6\xb5\xb7\xe8\xa7\x92\xe5\xa4\xa9\xe6\xb6\xaf"\n    PERFORMER "\xe7\x8e\x8b\xe8\x8a\xb7\xe8\x95\xbe"\n    INDEX 00 16:03:02\n    INDEX 01 16:06:07\n  TRACK 06 AUDIO\n    TITLE "\xe5\xbb\xbb"\n    PERFORMER "\xe6\x9d\x8e\xe6\x81\x95\xe6\x9d\x83"\n    INDEX 00 19:30:30\n    INDEX 01 19:33:22\n  TRACK 07 AUDIO\n    TITLE "\xe6\x95\x85\xe4\xba\x8b\xe7\x9a\x84\xe7\x9c\x9f\xe7\x9b\xb8"\n    PERFORMER "\xe9\xbb\x84\xe4\xbb\xb2\xe6\x98\x86\xe3\x80\x81\xe6\x9d\xa8\xe6\x9e\x97"\n    INDEX 00 24:04:05\n    INDEX 01 24:06:12\n  TRACK 08 AUDIO\n    TITLE "\xe6\x89\xac\xe5\xb8\x86"\n    PERFORMER "\xe9\x98\xb3\xe5\xb8\x86"\n    INDEX 00 28:34:42\n    INDEX 01 28:37:22']
作者: wujunkai    时间: 2020-4-4 13:38

本帖最后由 wujunkai 于 2020-4-4 13:59 编辑

回复 2# netdzb


    这才是正常的。。。。python 2.x对中文特别不友好。
  1. for i in songname:
  2.     print(i)
复制代码
好吧刚刚用python 3.8试了一下,python 对中文很不友好。。。。
作者: netdzb    时间: 2020-4-4 14:06

本帖最后由 netdzb 于 2020-4-4 14:08 编辑

回复 3# wujunkai


songtrim = '''PERFORMER "Various"
TITLE "宝丽金非常老牌精选"
FILE "CDImage.ape" WAVE
  TRACK 01 AUDIO
    TITLE "太阳一样"
    PERFORMER "刘文正"
    INDEX 01 00:00:00
  TRACK 02 AUDIO
    TITLE "堆积情感"
    PERFORMER "邝美云"
    INDEX 00 03:28:07
    INDEX 01 03:30:10
  TRACK 03 AUDIO
    TITLE "时空寄情"
    PERFORMER "黄莺莺"
    INDEX 00 08:24:00
    INDEX 01 08:27:12
  TRACK 04 AUDIO
    TITLE "再见我的爱人"
    PERFORMER "邓丽君"
    INDEX 00 12:33:10
    INDEX 01 12:36:32
  TRACK 05 AUDIO
    TITLE "海角天涯"
    PERFORMER "王芷蕾"
    INDEX 00 16:03:02
    INDEX 01 16:06:07
  TRACK 06 AUDIO
    TITLE "廻"
    PERFORMER "李恕权"
    INDEX 00 19:30:30
    INDEX 01 19:33:22
  TRACK 07 AUDIO
    TITLE "故事的真相"
    PERFORMER "黄仲昆、杨林"
    INDEX 00 24:04:05
    INDEX 01 24:06:12
  TRACK 08 AUDIO
    TITLE "扬帆"
    PERFORMER "阳帆"
    INDEX 00 28:34:42
    INDEX 01 28:37:22'''
songname = songtrim.split('\r')
#print(songname)
for line in songname:
       print (line)
#       if line.find('TRACK') == -1:
#              line = fin.readline()
#              continue
#              else:
       if line.find('TITLE') > -1:
              print(line.split('"')[-2])

py3我要倒出曲目表,我的代码为什么只能得到阳帆?
作者: wujunkai    时间: 2020-4-4 14:19

回复 4# netdzb
  1. for i in re.findall(r'(?<=TITLE ").+?(?=")',songtrim):
  2.     print(i)
复制代码
如果是对字符串进行提取,还是应该多用正则表达式。
  1. for i in songtrim.split('\n'):
  2. if(i.find('TITLE')!=-1):
  3. print(re.search('".+?"',i).group()[1:-1])
复制代码
另外,下次把代码贴上来的时候可以用[code][/code]括一下吗,手动选择真的不舒服




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2