在Textfile中搜索某些值并将其转换为字符串

时间:2017-10-18 13:55:52

标签: python python-2.7 python-3.x

如何从文本文件中读取多个值并将其转换为字符串以进行修改。 例如,我有TESTUSER.txt

Max Mustermann Kuerzel: MMUSTERMANN reads books
He lives in GERMANY
10 years ago he lived in POLAND
His Phone number is 0123456
His best friend was Peter, peter@testmail.com
His new best friend is Joseph, Joseph@example.com

我需要价值观德国,波兰,0123456,Peter,peter @ testmail.com,Joseph和Joseph@example.com。 文本的格式始终相同,唯一更改的值是上面给出的值。

1 个答案:

答案 0 :(得分:0)

遵循以下模式:

with open('t.txt') as file:
    lines = file.readlines()

for line in lines:
    if line.startswith('He lives in '):
        location = line.replace('He lives in ', '').strip()