繁体   English   中英

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

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

有没有更简单或更惯用的方法来写这个?

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

我不确定宏伟的计划是什么,但你可以用一种可能更快的方式来编写它:

在这里,假设您正在构建一个字符串:

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

顺便说一句,通过让大循环(在tf的所有字符上)由内置函数完成,并且只需迭代字符以排除:

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

而且更快:

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

设置(如果有人有兴趣寻找更快的方法):

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

对于您的特定目的(和学习),检查字符串中的特定字符将起作用。 python set检查成员资格更快。 您可以参考其他人的答案来了解如何做到这一点。 例如。

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

由于没有示例数据,我找不到更易读的名称,所以我保持不变。

您可能可以将这两个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