首页 > 解决方案 > 通过 def 标记化从字符串中删除

问题描述

尝试用

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.136278, -95.0406.

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

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

我得到以下信息:

latitude +41.137342,longitude=-95.947951bar=65.000000foo=23.849165secscolor=red latitude +41.137338,longitude=-95.947936bar=65.000000foo=0.036936secscolor=blue latitude +41.137338,longitude=-95.947936bar=65.000000foo=58.715930secscolor=蓝色的

期望的输出:

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

谢谢

标签: pythonparsing

解决方案


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

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

PS:可以多次使用:

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

推荐阅读