文字输出顺序错误

时间:2019-07-11 04:13:26

标签: python json

我有以下Python代码。

import json
temp_string = ""

with open("data_elements.json") as json_file:  
    data = json.load(json_file)
    for p in data:
        this_ldraw = p["ldraw"]
        this_lgeo = p["lgeo"]
        this_ldraw = this_ldraw.strip(".dat")
        this_lgeo = this_lgeo.strip("lg_")
        this_lgeo = this_lgeo.strip(".inc")
        print(this_ldraw + "\t" + this_lgeo)

我有一些JSON:

[
{"ldraw":"003238a.dat","lgeo":"lg_003238a.inc","slope":"0","anton":"0","lutz":"0","owen":"0","darats":"0"},
{"ldraw":"003238b.dat","lgeo":"lg_003238b.inc","slope":"0","anton":"0","lutz":"0","owen":"0","darats":"0"},
{"ldraw":"003238c.dat","lgeo":"lg_003238c.inc","slope":"0","anton":"0","lutz":"0","owen":"0","darats":"0"},
{"ldraw":"003238d.dat","lgeo":"lg_003238d.inc","slope":"0","anton":"0","lutz":"0","owen":"0","darats":"0"},
{"ldraw":"003238e.dat","lgeo":"lg_003238e.inc","slope":"0","anton":"0","lutz":"0","owen":"0","darats":"0"},
{"ldraw":"003238f.dat","lgeo":"lg_003238f.inc","slope":"0","anton":"0","lutz":"0","owen":"0","darats":"0"},
{"ldraw":"003238g.dat","lgeo":"lg_003238g.inc","slope":"0","anton":"0","lutz":"0","owen":"0","darats":"0"},
{"ldraw":"003238h.dat","lgeo":"lg_003238h.inc","slope":"0","anton":"0","lutz":"0","owen":"0","darats":"0"},
{"ldraw":"003238i.dat","lgeo":"lg_003238i.inc","slope":"0","anton":"0","lutz":"0","owen":"0","darats":"0"},
{"ldraw":"003238j.dat","lgeo":"lg_003238j.inc","slope":"0","anton":"0","lutz":"0","owen":"0","darats":"0"}
]

该Python脚本应该处理“ ldraw”列和“ lgeo”列,以便处理的结果是输出中的两个相同的列。但是,输出的顺序不正确。

输出看起来像这样:

003238  003238a
003238b 003238b
003238c 003238
003238  003238d
003238e 003238e
003238f 003238f
003238g 003238g
003238h 003238h
003238i 003238
003238j 003238j

我在做什么错?谢谢。

0 个答案:

没有答案