无法实现特定格式的JSON

时间:2017-07-17 20:50:35

标签: python json

我需要我的python文件以格式返回json输出。

{

"1": ["1503", "22939", "0", "0.0", "T"],

"2": ["850", "4895", "4", "1.647058823529412", "T"],

"3": ["337", "2748", "2", "2.3738872403560833", "T"],

"4": ["260", "2041", "2", "3.076923076923077", "T"]

}

我能得到的最好的是像这样的单个输出项

{"1": ["1503", "22939", "0", "0.0", "T"]}

{"2": ["850", "4895", "4", "1.647058823529412", "T"]}

{"3": ["337", "2748", "2", "2.3738872403560833", "T"]}

{"4": ["260", "2041", "2", "3.076923076923077", "T"]}

我的代码从CSV文件中读取并生成Json。

    # in an iterative for loop

    # Empty variable

    data = {}

    #lineCount is an incremental index , content List is a populated List

    data[lineCount] =contentList

    # Converting to a json format

    json_data = json.dumps(data)

    print(json_data)

我尝试了不同的方式但无法生成我想要的格式,我们非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

<script type="text/javascript"> 
   $( document ).ready(function() { 
     $( "#icon-hamburger" ).click(function() {
       $("#cell-phone").show();
 });
 });

</script>

在循环中执行此操作每次都会创建一个新词典,这解释了单项词典的列表。

只将你的dict初始化一次:

data = {}

#lineCount is an incremental index , content List is a populated List

data[lineCount] =contentList

# Converting to a json format

json_data = json.dumps(data)

然后循环并填充它

data = {}

然后在循环结束时,将其转储

for lineCount in some_iterable:
    ...
    data[lineCount] = contentList