如何把字符串中指定内容写TXT文件?
通过网络发包,返回一个str,格式如下:
{"retcode":0,
"result":
{
"friends":[
{"flag":0,"uin":2042918666,"categories":0},
{"flag":0,"uin":3742262888,"categories":0}
],
"marknames":[
{"uin":2042918666,"markname":"小A"},
{"uin":3742262888,"markname":"小B"}
],
"categories":[],
"vipinfo":[
{"vip_level":0,"u":2042918666,"is_vip":0}
,{"vip_level":0,"u":3742262888,"is_vip":0}
],
"info":[
{"face":216,"flag":8913410,"nick":"张三","uin":2042918666},
{"face":0,"flag":2155872256,"nick":"李四","uin":3742262888}
]
}}
=============================
想实现的功能,把"marknames"组中的内容写TXT文件,该如何实现呢?
2042918666@小A#
3742262888@小B#
[解决办法]
data = '''{"retcode":0,"result":{"friends":[{"flag":0,"uin":2042918666,"categories":0},{"flag":0,"uin":3742262888,"categories":0}],"marknames":[{"uin":2042918666,"markname":"小A"},{"uin":3742262888,"markname":"小B"}],"categories":[],"vipinfo":[{"vip_level":0,"u":2042918666,"is_vip":0},{"vip_level":0,"u":3742262888,"is_vip":0}],"info":[{"face":216,"flag":8913410,"nick":"张三","uin":2042918666},{"face":0,"flag":2155872256,"nick":"李四","uin":3742262888}]}}'''dic = eval(data)yourData = dic['result']['marknames']
[解决办法]
#转换字符串到字典格式,通过get(key)获取值content = eval(data).get('result').get('marknames')#写入txtfd = open(r'c:\target','w')fd.write(content)fd.close()