如何使用IsInstance Python排除JSON中的字符串的整数

发布于 2025-01-19 11:03:05 字数 1096 浏览 6 评论 0原文

我有一个包含所有字符串值的 json。我有一个解码器,它接受所有字符串并将它们解码为整数,但有一个值显示为“01”,我想将其保留为字符串。是否可以在解码器中排除它,使其保留为字符串而不是解析为整数。

我的 JSON 如下:

{"Data NC": "0", "Date": "10/10/2018", "Open Report": "142", "Date NC1": "40", "New Data": "0", "Some Report": "71", "New Data1": "01", "Date": "10/06/2021 00:00:00 AM"}

我已经尝试过使用:而不是 isinstance("New Data1", "01) 条件,但我没有运气。有没有办法做到这一点。代码如下:

class Decoder(json.JSONDecoder):
    def decode(self, s):
        result = super().decode(s)  # result = super(Decoder, self).decode(s) for Python 2.x
        return self._decode(result)

    def _decode(self, o):
        if isinstance(o, str):
            try:
                return int(o)
            except ValueError:
                return o
        elif isinstance(o, dict):
            return {k: self._decode(v) for k, v in o.items()}
        elif isinstance(o, list):
            return [self._decode(v) for v in o]
        else:
            return o

使用 json.loads 加载上面的解码器:

json_doc = json.loads(doc, cls=Decoder)

任何帮助将不胜感激。

I have a json that has all values of strings. I have a decoder that takes all the strings and decodes them into the integers, but there is one value that is showing as "01" that I would like to keep as a string. Is it possible to exclude this in the decoder for it to stay as a string and not parse as a integer.

My JSON is below:

{"Data NC": "0", "Date": "10/10/2018", "Open Report": "142", "Date NC1": "40", "New Data": "0", "Some Report": "71", "New Data1": "01", "Date": "10/06/2021 00:00:00 AM"}

I've already tried using: and not isinstance("New Data1", "01) condition, but I got no luck. Is there a way to do this. Code is below:

class Decoder(json.JSONDecoder):
    def decode(self, s):
        result = super().decode(s)  # result = super(Decoder, self).decode(s) for Python 2.x
        return self._decode(result)

    def _decode(self, o):
        if isinstance(o, str):
            try:
                return int(o)
            except ValueError:
                return o
        elif isinstance(o, dict):
            return {k: self._decode(v) for k, v in o.items()}
        elif isinstance(o, list):
            return [self._decode(v) for v in o]
        else:
            return o

Using json.loads to load the decoder above:

json_doc = json.loads(doc, cls=Decoder)

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海似她心 2025-01-26 11:03:05

您可以将此代码放在中尝试:除 -block:

result = int(o)
if str(result) == o:
    return result
else:
    return o

它将转换后的整数转换回字符串并将其与原件进行比较。如果它们相等,则一切都很好,如果不是,则返回字符串(因为整数已领先0,不应转换)。单个0转换为整数。输出将是:

{
  'Data NC': 0,
  'Date': '10/06/2021 00:00:00 AM',
  'Open Report': 142,
  'Date NC1': 40,
  'New Data': 0,
  'Some Report': 71,
  'New Data1': '01'
}

You can put this code in your try: except-block:

result = int(o)
if str(result) == o:
    return result
else:
    return o

it converts the converted integer back to string and compares it with the original. If they are equal all is fine, if not it returns the string (because the integer has leading 0s and should not be converted). Single 0s were converted to integers. The output will be:

{
  'Data NC': 0,
  'Date': '10/06/2021 00:00:00 AM',
  'Open Report': 142,
  'Date NC1': 40,
  'New Data': 0,
  'Some Report': 71,
  'New Data1': '01'
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文