通过def-tokenized从字符串中删除

时间:2018-07-05 19:58:47

标签: python parsing

尝试标记化和剥离逗号值

的当前输出
def clean(string):
result = ""
for i,char in enumerate(line):
    if char == " ":
        if string[i+1].isdigit() or string[i+1] == " ":
            continue
    result += char
return result
  

纬度+41.136778,-95.948622 65.000000红色   纬度+41.136778,-95.948622 65.000000红色   纬度+41.136778,-95.948622 65.000000蓝色
  纬度+41.136778,-95.948622 65.000000红色

如果我尝试这样在字符串中添加逗号:

def clean(string):
result = ""
for i,char in enumerate(line):
    if char == " ":
        if string[i+1].isdigit() or or string[i+1] + "," string[i+1] == " ":
            continue
    result += char
return result

我得到以下信息:

  

纬度+41.137342,经度= -95.947951bar = 65.000000foo = 23.849165secscolor =红色纬度+41.137338,经度= -95.947936bar = 65.000000foo = 0.036936secscolor =蓝色纬度+41.137338,经度= -95.947936bar = 65.000000foo = 58.715930secscolor =蓝色

所需的输出:

  

纬度+41.136778经度栏-95.948622 foo 65.000000颜色蓝色      纬度+41.136778经度栏-95.948622 foo 65.000000颜色红色

谢谢

1 个答案:

答案 0 :(得分:1)

不确定是否完全理解,但我认为您可能正在寻找以下内置功能:

return string.replace(",", " ")

PS:您可以多次使用它:

return string.replace(",", " ").replace("=", " ")