Python提取中文关键词方法,,我想从一组中文字符串中取


我想从一组中文字符串中取得关键词,如“出版社”,然后将“朝华出版社”赋值给publisher。但是不知道应该怎么做,已经做了如下尝试。希望能够得到解答。

python>>>import sys>>>reload(sys)>>>sys.setdefaultencoding('utf8')>>>>>>import re>>>text = '''出版社: 朝华出版社出版年: 2007-12页数: 752定价: 49.80元装帧: 平装ISBN: 9787505417670'''>>>re.findall(r'出版社:(.*?)'.encode('utf-8'), text.encode('utf-8'))['']
pythontranslations = {  '出版社': 'publisher',  #...}text = '''出版社: 朝华出版社出版年: 2007-12页数: 752定价: 49.80元装帧: 平装ISBN: 9787505417670'''data = {}for l in text.strip().splitlines():  k, v = l.split(': ', 1)  data[translations.get(k, k)] = vprint(data)

你问的是 Y 问题。以上是我猜测出来的 X 问题。参见:X-Y Problem | 酷 壳 - CoolShell.cn

print re.findall(r'出版社: ([\s\S]*?)\n', text)[0]

编橙之家文章,

评论关闭