利用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'))
['']
阅读 12.1k
3 个回答

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

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)] = v
print(data)

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

新手上路,请多包涵

可以使用json库:

import json
data=json.load(text)
publisher=data["出版社"]
推荐问题