Python 使用逗号拆分逗号分隔的键值对

Python 使用逗号拆分逗号分隔的键值对,python,parsing,Python,Parsing,有点像这个问题: 但我的问题是: line='name=zhg,code=#123,"text=hello,boy"' 注意,“text=hello,boy”,而不是:text=“hello,boy” 我想把这一行分开听写。 我想要的输出是: "name":"zhg","code":"#123","text":"hello,boy" 如何使用regex或shlex获取它?使用regex无法做到这一点,否则它将不是最有效的。使用单通道解析器解析此类字符串的代码非常简单: line='name

有点像这个问题:

但我的问题是:

line='name=zhg,code=#123,"text=hello,boy"'
注意,“text=hello,boy”,而不是:text=“hello,boy”

我想把这一行分开听写。 我想要的输出是:

"name":"zhg","code":"#123","text":"hello,boy"

如何使用regex或shlex获取它?

使用regex无法做到这一点,否则它将不是最有效的。使用单通道解析器解析此类字符串的代码非常简单:

line='name=zhg,code=#123,"text=hello,boy"'


def read_quote(string):
    out = ''
    for index, char in enumerate(string):
        if char == '"':
            index += 2  # skip quote and comma if any
            return index, out
        else:
            out += char


def read(string):
    print('input', string)
    out = ''
    for index, char in enumerate(string):
        if char == ',':
            index += 1  # skip comma
            return index, out
        else:
            out += char
    # end of string
    return index, out

def components(string):
    index = 0
    while index < len(line):
        if string[index] == '"':
            inc, out = read_quote(string[index+1:])
            index += inc
            yield out
        else:
            inc, out = read(string[index:])
            index += inc
            yield out

print(dict([e.split('=') for e in components(line)]))

如果你真的想,你可以使用正则表达式来实现
read
read

你不能用正则表达式来实现这一点,否则它不会是最有效的。使用单通道解析器解析此类字符串的代码非常简单:

line='name=zhg,code=#123,"text=hello,boy"'


def read_quote(string):
    out = ''
    for index, char in enumerate(string):
        if char == '"':
            index += 2  # skip quote and comma if any
            return index, out
        else:
            out += char


def read(string):
    print('input', string)
    out = ''
    for index, char in enumerate(string):
        if char == ',':
            index += 1  # skip comma
            return index, out
        else:
            out += char
    # end of string
    return index, out

def components(string):
    index = 0
    while index < len(line):
        if string[index] == '"':
            inc, out = read_quote(string[index+1:])
            index += inc
            yield out
        else:
            inc, out = read(string[index:])
            index += inc
            yield out

print(dict([e.split('=') for e in components(line)]))

如果您确实愿意,您可以使用正则表达式来实现
read
read

您可以使用带有适当的“类似文件”字符串的
csv.reader


您可以将
csv.reader
与适当的“类似文件”字符串一起使用


预期的输出是什么?我想你的意思是
line='name=zhg,code=#123,text=“你好,孩子”
@AvinashRaj:我想他不会。如果他这样做了,他的链接将回答他的问题。预期的结果是什么?你是指
line='name=zhg,code=#123,text=“你好,孩子”
@AvinashRaj:我想他不会。如果他这样做了,他的链接将回答他的问题。代码不做验证,也不做错误检查。代码不做验证,也不做错误检查。谢谢!然后我可以使用str.split('=')得到我想要的。谢谢!然后我可以使用str.split('=')获得我想要的。
>>> import csv
>>> import StringIO
>>> line='name=zhg,code=#123,"text=hello,boy"'
>>> string_file = StringIO.StringIO(line)
>>> for row in csv.reader(string_file):
...  print row
...
['name=zhg', 'code=#123', 'text=hello,boy']