python - 通过 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 颜色红色
谢谢
解决方案
不确定是否完全理解,但我认为以下内置函数可能是您正在寻找的:
return string.replace(",", " ")
PS:可以多次使用:
return string.replace(",", " ").replace("=", " ")
推荐阅读
- swift - 无法在 Xcode 中启用使用 Apple 登录
- android - 如何为 android API 22 编译 opencv 库
- python - Python:按其他数组排列数组
- android - 在传统的 android ViewPager 中滚动 LazyRow 的问题
- r - 将 SPSS (.sav) 数据集读入 R 并出现错误
- acumatica - 自定义签名签名功能
- java - Java Lambda 捕获还是重新执行?
- html - @Input 表单子组件
- java - 反应原生:无法 HEAD 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/4.1.0/gradle-4.1.0.pom'
- java - 如何在 Maven 项目中部署 Angular?