楼主: fengweilong
926 4

[统计软件与数据分析] 求问Python如何把下列字符串中的中文以列表形式提取出来? [推广有奖]

  • 0关注
  • 0粉丝

大专生

83%

还不是VIP/贵宾

-

威望
0
论坛币
492 个
通用积分
2.6227
学术水平
8 点
热心指数
8 点
信用等级
8 点
经验
555 点
帖子
17
精华
0
在线时间
88 小时
注册时间
2019-2-26
最后登录
2023-6-15

100论坛币
{"code":"0","data":{"block":[{"type":"text","line":[{"confidence":1,"word":[{"content":"王诗韵"}]},{"confidence":1,"word":[{"content":"王诗韵"}]},{"confidence":1,"word":[{"content":"王诗韵"}]},{"confidence":1,"word":[{"content":"王石峰"}]},{"confidence":1,"word":[{"content":"王石峰"}]},{"confidence":1,"word":[{"content":"王石彤"}]},{"confidence":1,"word":[{"content":"王士曼"}]},{"confidence":1,"word":[{"content":"王士曼"}]},{"confidence":1,"word":[{"content":"王士宁"}]},{"confidence":1,"word":[{"content":"王士宁"}]},{"confidence":1,"word":[{"content":"王士英"}]},{"confidence":1,"word":[{"content":"王士英"}]},{"confidence":1,"word":[{"content":"王士英"}]},{"confidence":1,"word":[{"content":"王士英"}]},{"confidence":1,"word":[{"content":"王士勇"}]},{"confidence":1,"word":[{"content":"王士勇"}]},{"confidence":1,"word":[{"content":"王世博"}]},{"confidence":1,"word":[{"content":"王世博"}]},{"confidence":1,"word":[{"content":"王世博"}]},{"confidence":1,"word":[{"content":"王世春"}]},{"confidence":1,"word":[{"content":"王世春"}]},{"confidence":1,"word":[{"content":"王世春"}]},{"confidence":1,"word":[{"content":"王世春"}]},{"confidence":1,"word":[{"content":"王世锋"}]},{"confidence":1,"word":[{"content":"王世红"}]},{"confidence":1,"word":[{"content":"王世宏"}]},{"confidence":1,"word":[{"content":"王世宏"}]},{"confidence":1,"word":[{"content":"王世宏"}]},{"confidence":1,"word":[{"content":"王世宏"}]},{"confidence":1,"word":[{"content":"王世宏"}]},{"confidence":1,"word":[{"content":"王世杰"}]},{"confidence":1,"word":[{"content":"王世杰"}]},{"confidence":1,"word":[{"content":"王世杰"}]},{"confidence":1,"word":[{"content":"王世杰"}]}]}]},"desc":"success","sid":"wcr0015ed0d@gza6ea141a4a93463000"}

不一定是中文,那一部分的内容可能是中文也可能是英文,主要是把“”内的内容提取出来

关键词:字符串 confidence Content Success dence Python
建议问题描述:原始例子+结果例子,你这么描述估计没人看得懂你要什么吧

使用道具

藤椅
JoeyKi 发表于 2021-6-11 09:32:47 |只看作者 |坛友微信交流群
# -*- Coding: UTF-8 -*
#dict0就是原始储存数据的那个的字典
dict0={"code":"0","data":{"block":[ {"type":"text","line"... ,"desc":"success","sid":"wcr0015ed0d@gza6ea141a4a93463000"}
content=[]
#提取content的内容
for x in dict0["data"]["block"][0]["line"]:
    content.append (x["word"][0]["content"])

#提取desc的内容
content.append (dict0["desc"])
#提取sid的内容
content.append (dict0["sid"])
#转为unicode编码,string-escape去掉转义字符
content=str (content).decode ("string_escape")
print(content)

828AF056-5D07-4245-A5A9-6AF68A7CA087.jpeg (585.91 KB)

828AF056-5D07-4245-A5A9-6AF68A7CA087.jpeg

使用道具

板凳
causs_x 在职认证  发表于 2021-6-13 11:47:33 |只看作者 |坛友微信交流群
用正则表达式提取非常简单
import re

str0 = '
{"code":"0","data":{"block":[{"type":"text","line":[{"confidence":1,"word":[{"content":"王诗韵"}]},{"confidence":1,"word":[{"content":"王诗韵"}]},{"confidence":1,"word":[{"content":"王诗韵"}]}}}'

pattern = '[\u4e00-\u9fa5]'
res_list = re.findall(pattern,str0)
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
admin_kefu + 30 + 3 + 3 + 3 精彩帖子

总评分: 论坛币 + 30  学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

使用道具

报纸
dawawayu 发表于 2021-6-29 17:41:15 |只看作者 |坛友微信交流群
标准化的 做法是import  json。
使用 json 的loads, dump 方法 可以把字典以及字典字符串 相互转化。

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-15 02:44