使用python获取文本文件中的特定单词

使用python获取文本文件中的特定单词,python,python-2.7,Python,Python 2.7,我有一个文本文件包含这种格式的“a 0001 1212 00@@word”,我想在访问word时打印,然后只有0001 1212部分必须与00一起显示,还必须动态显示您尝试了什么?我不是真的遵循你的格式 f=open(filepath, 'r') hold = f.read() #hold now = 'A 0001 1212 00 @@ word' holdout = hold.replace( '@@ word','') #holdout now = 'A 0001 1212 00 ' #t

我有一个文本文件包含这种格式的“a 0001 1212 00@@word”,我想在访问word时打印,然后只有0001 1212部分必须与00一起显示,还必须动态显示

您尝试了什么?我不是真的遵循你的格式

f=open(filepath, 'r')
hold = f.read()
#hold now = 'A 0001 1212 00 @@ word'
holdout = hold.replace( '@@ word','')
#holdout now = 'A 0001 1212 00 '
#the replace call uses this format 
#string.replace('what it is looking for', 'what it is going to be')

这是你想要的吗?此文件中是否只有一行?

只有文本文件包含“0001 1212 00@@word”确定当我尝试提取“word”时,输出文件中应显示0001 1212文本您可以将数据作为字符串导入,然后执行字符串-“word”吗。这就是您在输出中寻找的吗?你能做一个格式化前和格式化后的例子吗?(你应该把这个编辑成你的问题)。@deadstump:我很确定你不能减去那样的字符串。