查找某些字符串后存储的数据 - Python

时间:2017-09-11 09:21:07

标签: python

我试图找到一种方法,使用python将数据存储在.txt文件中某个字符串的末尾。 E.g:

bob: 22
jeff: 35

我怎么能告诉python要求bobs年龄。不是整个字符串。

由于 托林

1 个答案:

答案 0 :(得分:0)

如果a.txt文件的内容类似于

bob: 22
jeff: 35

然后这段代码应该做你的工作

file1 = open("a.txt","r") # open the file for reading
lines = file1.readlines() #get each line by readlines to a array of lines
for line in lines:
    data = line.split(":")[1] # split the line with : so the second part is
 # the data that you want to reach