简体   繁体   English

如何使 json 解析校验码更短或更易读?

[英]How to make json parsing check code shorter or more readable?

Is there a simpler or more idiomatic way to write this?有没有更简单或更惯用的方法来写这个?

def jsonToCsvPart1(fields):
    csvFormat = ''
    fields = json.loads(fields)
    for key in fields.keys() :
        tf = str(fields[key])
        mk = ''
        for idx in tf :
            if idx == '{' or idx == '}' or idx == '[' or idx == ']' :
                continue
            else :
               mk += idx
        csvFormat += (mk + ",")
    yield csvFormat

I'm not sure what the grand scheme is, but you can write it in a way that will likely be faster:我不确定宏伟的计划是什么,但你可以用一种可能更快的方式来编写它:

Here, assuming you are building a string:在这里,假设您正在构建一个字符串:

exclude = set(list('{}[]'))  # note: set is faster than list for membership test
mk = ''.join([idx for idx in tf if idx not in exclude])
# 61 ms on a 1M-char random string
exclude = '{}[]'  # ...but string is even faster, when so short
mk = ''.join([idx for idx in tf if idx not in exclude])
# 38 ms on a 1M-char random string

By the way, it will be considerably faster to achieve the same by letting the large loop (on all chars of tf ) be done by builtin functions, and just iterate on the chars to exclude:顺便说一句,通过让大循环(在tf的所有字符上)由内置函数完成,并且只需迭代字符以排除:

mk = tf.replace('{', '').replace('}', '').replace('[', '').replace(']', '')
# 11.8ms on 1M chars

And yet faster:而且更快:

mk = tf.translate({ord(c): None for c in '{}[]'})
# 4.5 ms on 1M chars

Setup (if anyone is interested to look for yet a faster way):设置(如果有人有兴趣寻找更快的方法):

tf = ''.join(np.random.choice(list('abcdefghijk{}[]'), size=1_000_000))

For your specific purpose (and learning), checking specific chars in a string will work.对于您的特定目的(和学习),检查字符串中的特定字符将起作用。 python set are faster for checking membership. python set检查成员资格更快。 You can refer to others's answers on how to do that.您可以参考其他人的答案来了解如何做到这一点。 eg.例如。

idxs = '{}[]'
for idx in tf:
    if idx in idxs:
        continue
    else:
        mk += idx

I cannot find more readable names as there is no sample data, so I keep them untouched.由于没有示例数据,我找不到更易读的名称,所以我保持不变。

You could probably nest these two for loops together, to get even less lines, but poor readability IMHO.您可能可以将这两个for循环嵌套在一起,以获得更少的行数,但恕我直言,可读性较差。

def jsonToCsvPart1(fields):
    csvFormats = []
    for tf in json.loads(fields):
        mk = ''.join(str(idx) for idx in tf if str(idx) not in '{}[]')
        csvFormats.append(mk)
    yield ','.join(csvFormats)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM