当前位置: 首页 > news >正文

易经网站开发公司中国建设银行网站公积金查询

易经网站开发公司,中国建设银行网站公积金查询,自己做qq头像静态的网站,镇江网红景点正如问题所解释的那样#xff0c;我一直在尝试生成嵌套的JSON对象。在这种情况下#xff0c;我有for循环从字典dic中获取数据。下面是代码#xff1a; f open(test_json.txt, w) flag False temp start {\n\t\filename\我一直在尝试生成嵌套的JSON对象。在这种情况下我有for循环从字典dic中获取数据。下面是代码 f open(test_json.txt, w) flag False temp start {\n\t\filename\ : \ initial_filename\,\n\t\data\ : [\n end \n\t] \n} f.write(start) for i, (key,value) in enumerate(dic.iteritems()): f.write({\n\t\keyword\:\str(key)\,\n) f.write(\term_freq\:str(len(value)),\n) f.write(\lists\:[\n\t) for item in value: f.write({\n) f.write(\t\t\occurance\ :str(item)\n) #Check last object if value.index(item)1 len(value): f.write(}\n f.write(]\n) else: f.write(},) # close occurrence object # Check last item in dic if i len(dic)-1: flag True if(flag): f.write(}) else: f.write(},) #close lists object flag False #check for flag f.write(]) #close lists array f.write(}) 预期输出为 { filename: abc.pdf, data: [{ keyword: irritation, term_freq: 5, lists: [{ occurance: 1 }, { occurance: 1 }, { occurance: 1 }, { occurance: 1 }, { occurance: 2 }] }, { keyword: bomber, lists: [{ occurance: 1 }, { occurance: 1 }, { occurance: 1 }, { occurance: 1 }, { occurance: 2 }], term_freq: 5 }] } 但是目前我得到如下输出 { filename: abc.pdf, data: [{ keyword: irritation, term_freq: 5, lists: [{ occurance: 1 }, { occurance: 1 }, { occurance: 1 }, { occurance: 1 }, { occurance: 2 },] // Here lies the problem , before array(last element) }, { keyword: bomber, lists: [{ occurance: 1 }, { occurance: 1 }, { occurance: 1 }, { occurance: 1 }, { occurance: 2 },], // Here lies the problem , before array(last element) term_freq: 5 }] } 请帮助我正在尝试解决它但是失败了。请不要将其标记为重复因为我已经检查了其他答案并且完全没有帮助。 编辑1 输入基本上是从映射类型为dic的字典中获取的 例如“刺激” [1、3、5、7、8] 其中刺激是关键并映射到页码列表。 这基本上是在外部for循环中读取的其中key是关键字value是该关键字出现的页面列表。 编辑2 dic collections.defaultdict(list) # declaring the variable dictionary dic[key].append(value) # inserting the values - useless to tell here for key in dic: # Here dic[x] represents list - each value of x print key,:,dic[x],\n #prints the data in dictionary 参考方案 andrea -f对我来说看起来不错这是另一种解决方案 随意选择两者:) import json dic { bomber: [1, 2, 3, 4, 5], irritation: [1, 3, 5, 7, 8] } filename abc.pdf json_dict {} data [] for k, v in dic.iteritems(): tmp_dict {} tmp_dict[keyword] k tmp_dict[term_freq] len(v) tmp_dict[lists] [{occurrance: i} for i in v] data.append(tmp_dict) json_dict[filename] filename json_dict[data] data with open(abc.json, w) as outfile: json.dump(json_dict, outfile, indent4, sort_keysTrue) 同样的想法我首先创建一个大的json_dict以直接保存在json中。我使用with语句保存json以避免捕获exception 另外如果将来需要改进json.dumps()输出则应该查看json的文档。 编辑 而且只是为了好玩如果您不喜欢tmp var则可以单行处理所有数据for循环 json_dict[data] [{keyword: k, term_freq: len(v), lists: [{occurrance: i} for i in v]} for k, v in dic.iteritems()] 它可以为最终解决方案提供一些不完全可读的内容例如 import json json_dict { filename: abc.pdf, data: [{ keyword: k, term_freq: len(v), lists: [{occurrance: i} for i in v] } for k, v in dic.iteritems()] } with open(abc.json, w) as outfile: json.dump(json_dict, outfile, indent4, sort_keysTrue) 编辑2 看来您不想将json保存为所需的输出但还是想读取它。 实际上您也可以使用json.dumps()来打印json。 with open(abc.json, r) as handle: new_json_dict json.load(handle) print json.dumps(json_dict, indent4, sort_keysTrue) 但是这里仍然存在一个问题因为列表中filename:的d在data之前所以f会打印在列表的末尾。 要强制命令您将必须在字典的生成中使用OrderedDict。请注意python 2.X的语法很丑imo 这是新的完整解决方案; import json from collections import OrderedDict dic { bomber: [1, 2, 3, 4, 5], irritation: [1, 3, 5, 7, 8] } json_dict OrderedDict([ (filename, abc.pdf), (data, [ OrderedDict([ (keyword, k), (term_freq, len(v)), (lists, [{occurrance: i} for i in v]) ]) for k, v in dic.iteritems()]) ]) with open(abc.json, w) as outfile: json.dump(json_dict, outfile) # Now to read the orderer json file with open(abc.json, r) as handle: new_json_dict json.load(handle, object_pairs_hookOrderedDict) print json.dumps(json_dict, indent4) 将输出 { filename: abc.pdf, data: [ { keyword: bomber, term_freq: 5, lists: [ { occurrance: 1 }, { occurrance: 2 }, { occurrance: 3 }, { occurrance: 4 }, { occurrance: 5 } ] }, { keyword: irritation, term_freq: 5, lists: [ { occurrance: 1 }, { occurrance: 3 }, { occurrance: 5 }, { occurrance: 7 }, { occurrance: 8 } ] } ] } 但是要小心在大多数情况下最好保存常规的.json文件以使它们成为跨语言的。 Python uuid4如何限制唯一字符的长度 - python 在Python中我正在使用uuid4方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试x uuid4() str(x)[:8] 输出ffc69c1b Is there a way to…json.dumps弄乱顺序 - python 我正在使用json module创建一个包含类似条目的json文件json.dumps({fields: { name: %s, city: %s, status: %s, cou…JSON SCHEMA PATTERN逗号分隔列表 - python 我的json模式中具有以下模式并且我需要根据以下模式包含逗号分隔的值。当前模式只能像DV2一样处理一种模式所以我应该如何修改我的模式以包括多个字符串如下所示但它应该与声明的模式匹配。例如“ DV2”“ DEV1”“ DEV3”“ ST” ENVIRONMENT: { type: st…Python中的Json操作 - python Latest_json和Historic_json函数返回return(frame.to_json(orientrecords)) 主功能recentdata recent_json(station) historicdata historic_json(station) alldata historicdata …Python-crontab模块 - python 我正在尝试在Linux OSCentOS 7上使用Python-crontab模块我的配置文件如下{ ossConfigurationData: { work1: [ { cronInterval: 0 0 0 1 1 ?, attribute…
http://www.pierceye.com/news/768132/

相关文章:

  • 网站维护明细报价表最新的网站建设架构
  • 百度大全seo推广话术
  • 做网站赚钱流程英文网站建设注意什么
  • 腾讯 云上做网站教程开源系统 网站
  • 临沂罗庄做网站服装商城网站建设价格
  • 保定企业官网搭建对网站有效的优化软件
  • 网站后台代码在哪修改股权众筹网站建设
  • 站群源码北京公司注销
  • 营销型网站策划建设台州市住房和城乡建设厅网站
  • 达内网站开发课程wordpress自动添加标签页
  • 免费的个人网站空间我做淘宝网站卖东西怎么激活
  • 织梦dedecms女性时尚门户网站模板常州网络推广平台
  • 网站怎么在百度搜不到资源网源码
  • 怎样网站制作设计广西住房城乡建设部官网
  • 手机网站建设方案书王烨是哪个小说的主角
  • 临沂网站案例百事可乐网络营销推广方法
  • 广州网站建设信科网络冷水滩网站建设
  • 做网站设计的价格企业seo网站营销推广
  • 河南省住房和建设厅门户网站网站开发图片素材
  • 在线代理浏览器网站设计本笔记本
  • gta5网站正在建设中柳州团购汽车网站建设
  • 建设一个网站要多少费用吗wordpress 缓存首页
  • 绵阳网站排名深圳哪家网页设计好
  • 软件 开发公司宿迁seo优化
  • 网站开发demo版本做网站服务器的配置
  • 网页游戏排行2013伊克昭盟seo
  • 单页站如何做网站seo优化建e网卧室设计效果图
  • 免费做网站的app巩义seo
  • 做金融服务网站赚钱阿里巴巴网站建设论文
  • 四川做网站的公司哪家好免费团购网站模板