python-3.x - 从 Python 3.7 中的字符串中提取特定信息
问题描述
Python3.7
以下是我的输入:
一条“随机道路” (1,2) (2,3) (3,4)
a 是我添加道路的命令。接下来是道路名称及其位置。我需要提取道路的名称。
我希望提取道路名称及其坐标并存储在单独的列表中。我可以使用 re 提取整数,但无法提取道路名称。如何仅提取道路名称并将其存储在单独的字符串中。
解决方案
也许使用拆分方法?
test_str = "Random Road (1,2) (2,3)(3,4)"
print(test_str.split("(")[0].strip())
'Random Road'
编辑:如果道路名称在引号之间,则添加更简单的方法
import re
test_str = """a "Random Road" (1,2) (2,3)(3,4)"""
print(re.findall('"([^"]*)"', test_str))
['Random Road']
推荐阅读
- python - 在深度学习 keras 的损失函数中添加中间层
- c++ - 动态规划状态计算
- reactjs - React 容器的 props 更改导致 Uncaught TypeError: Cannot read property of undefined
- javascript - jquery mobile listview没有格式化列表
- python - 从存储在python列表中的字符串中清除数据
- c++ - C++子类继承
- python - 从另一个数据帧中减去一个数据帧,不包括第一列 Pandas
- javascript - 在 javascript 中查找常用字符串
- osgi - 将 Sling 调度程序与 Eclipse Equinox 或 OSGI @Reference 规则一起使用
- facebook-graph-api - 获取托管页面列表