Python字符串拆分,处理单引号

时间:2019-01-04 11:14:36

标签: python string list

我正在尝试用“,”分割字符串。 按预期,“拆分”功能可用于以下“示例1”。

example1 = "1,'aaa',337.5,17195,.02,0,0,'yes','abc'"
example1.split(",")
Result: ['1', "'aaa'", '337.5', '17195', '.02', '0', '0', "'yes'", "'abc'"]

但是,在这里,我有一个场景,单引号内有逗号,我不想在上面加分号。

example2 = "1,'aaa',337.5,17195,.02,0,0,'yes','abc, def, xyz'"
example2.split(",")
Result: ["1,'aaa',337.5,17195,.02,0,0,'yes','abc,", 'def,', "xyz'"]

但是我正尝试获得此结果:

['1', "'aaa'", '337.5', '17195', '.02', '0', '0', "'yes'", "'abc, def, xyz'"]

如何使用字符串拆分功能实现这一目标?

2 个答案:

答案 0 :(得分:7)

您应该首先尝试使用内置程序或标准库将数据作为列表读入 ,例如,直接通过csv模块从CSV文件中读取数据。

>

如果您的字符串来自无法控制的来源,则添加方括号和右方括号将提供有效的list,因此您可以使用ast.literal_eval

from ast import literal_eval

example2 = "1,'aaa',337.5,17195,.02,0,0,'yes','abc, def, xyz'"

res = literal_eval(f'[{example2}]')

# [1, 'aaa', 337.5, 17195, 0.02, 0, 0, 'yes', 'abc, def, xyz']

这确实将数字数据转换为整数/浮点数。如果您希望按照@JonClements的注释将它们保留为字符串,则可以传递给csv.reader

import csv

res = next(csv.reader([example2], quotechar="'")) 

# ['1', 'aaa', '337.5', '17195', '.02', '0', '0', 'yes', 'abc, def, xyz']

答案 1 :(得分:0)

假设您希望将'保留在元素周围("'aaa'"而不是预期输出中的'aaa'),可以通过以下方法使用函数: / p>

def spl(st, ch):
  res = []
  temp = []
  in_quote = False
  for x in st:
    if (x == "'"):
      in_quote = not in_quote

    if (not in_quote and x == ch):
      res.append("".join(temp))
      temp = []
    else:
      temp.append(x)

  res.append("".join(temp))
  return res




example2 = "1,'aaa',337.5,17195,.02,0,0,'yes','abc, def, xyz'"

print(spl(example2, ','))

输出:

['1', "'aaa'", '337.5', '17195', '.02', '0', '0', "'yes'", "'abc, def, xyz'"]
相关问题