首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

怎么把字符串中指定内容写TXT文件

2012-03-06 
如何把字符串中指定内容写TXT文件?通过网络发包,返回一个str,格式如下:{retcode:0,result:{friends:

如何把字符串中指定内容写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#

[解决办法]

Python code
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']
[解决办法]
探讨

Python code
data = '''
{"retcode":0,
"result":
{
"friends":[
{"flag":0,"uin":2042918666,"categories":0},
{"flag":0,"uin":3742262888,"categories":0}
],

"marknames":[
{"uin":2042918666,"markname":"小A……

[解决办法]
" “
[解决办法]
#1楼的回复不对,不能把字典当做序列的概念来处理。楼主可能需要理解下python中的“字典”,这类似于C#中的Json格式:
Python code
#转换字符串到字典格式,通过get(key)获取值content = eval(data).get('result').get('marknames')#写入txtfd = open(r'c:\target','w')fd.write(content)fd.close() 

热点排行