python 字符串操作

标签 python

要重新表述问题。

基本上我想知道操作如下格式的字符串的最简单方法是什么:

安全/报告/图像/489

安全/报告/图像/490

并用斜杠(/)分隔每个单词,并将每个部分( token )存储到存储中,以便我稍后可以调用它。 (从 CSV 文件中读取约 1200 个单元格)。

最佳答案

您问题的答案:

>>> mystring = "Safety/Report/Image/489"
>>> mystore = mystring.split('/')
>>> mystore
['Safety', 'Report', 'Image', '489']
>>> mystore[2]
'Image'
>>> 

如果您想存储多个字符串的数据,那么您有多种选择,具体取决于您想要如何组织它。例如:

liststring = ["Safety/Report/Image/489",
              "Safety/Report/Image/490",
              "Safety/Report/Image/491"]

dictstore = {}            
for line, string in enumerate(liststring):
    dictstore[line] = string.split('/')

print dictstore[1][3]
print dictstore[2][3]

prints:
490
491

在这种情况下你可以用同样的方式使用字典或者列表(列表的列表)来存储。如果每个字符串都有一个特殊的标识符(比行号更好),则可以选择字典。

关于python 字符串操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6872718/

相关文章:

python - 在 pythoncurses 中使用退格键和 getch()

python - AWS 中针对 Anaconda python 的 Amazon 机器镜像

python - 在 tkinter 标签 Python 中显示平方根符号

python - Matplotlib - 如何在不使线条透明的情况下使标记面颜色透明

java - 根据同一行中另一个元素的条件单击表中的元素

python - 无法安装和构建 PyAudio

python - TLS MAC 消息验证

python - 这是一种在 python 中编码和解码字符串的安全方法吗?

python - 在python中的elementTree中插入xml条目

python - python中的多维数组到数据库