-1

我有一个文件包含

"1111 1111 1111 // google 
1111 1111 1111 // google talk browser 
1111 1111 1111 // google talks 
1111 1111 1111 // google talk" 

我只想打印“// google talk”相关行(仅第 4 行)

像这样试过这不起作用......

with open('file.txt', 'r') as handle:
    for index, line in enumerate(handle, 1):
        if line.strip() == 'talk':
            print 'Match found on line', index
4

1 回答 1

3
with open('file.txt', 'r') as handle:
    for index, line in enumerate(handle, 1):
        if line.rstrip().endswith("// google talk") # or .endswith("talk")
            print 'Match found on line', index
于 2012-10-04T06:59:51.167 回答