字符串匹配和替换

String matching and replace

我有一个这样的文本文件,我想在 python

中实现
Enter the Username"<Username>" and phonenumber"<phonenumber>"
Enter the origin"<origin>" and destination"<destination>"
Examples:
 | Username | phonenumber | origin | destination|
 | JOHN | 40256786 | NYC | LONDON |

我想替换 <> 中的字符串并替换为实际数据,我的输出将如下所示:

Enter the Username "JOHN" and phonenumber "40256786"
Enter the origin "NYC" and destination "LONDON"

更新

尝试:

import re

text = []
data = []
with open('data.txt') as fp:
    line = ''
    for line in fp:
        if line.startswith('Examples'):
            break
        text.append(line)
    text = ''.join(text)
    headers = re.split('\s*\|\s*', fp.readline())[1:-1]
    for line in fp:
        values = re.split('\s*\|\s*', line)[1:-1]
        data.append(dict(zip(headers, values)))

for d in data:
    print(re.sub(r'\<(?P<key>[^>]*)\>', lambda x: d[x.group('key')], text))

输出:

Enter the Username"JOHN" and phonenumber"40256786"
Enter the origin"NYC" and destination"LONDON"

旧答案

您可以使用大量文本处理器通过变量替换文本:string.Template ($)、format strings ({ })、Jinja2 ( {{ }})。如果可以,请更改分隔符:

这里是格式字符串的例子:

text = '''\
Enter the Username "{Username}" and phonenumber "{phonenumber}"
Enter the origin "{origin}" and destination "{destination}"\
'''

data = {'Username': 'John', 'phonenumber': '40256786',
        'origin': 'NYC', 'destination': 'LONDON'}

print(text.format(**data))

输出:

Enter the Username "John" and phonenumber "40256786"
Enter the origin "NYC" and destination "LONDON"

一种方法是用分隔符 | 分隔每一行。然后你可以相应地为字符串设置变量。

sample_line = '| JOHN | 40256786 | NYC | LONDON |'
sample_line = sample_line.split('|')

data = {
    'Username':     sample_line[1],
    'phonenumber':  sample_line[2],
    'origin':       sample_line[3],
    'destination':  sample_line[4]
    }

text = '''\
Enter the Username "{Username}" and phonenumber "{phonenumber}"
Enter the origin "{origin}" and destination "{destination}"\
'''

print(text.format(**data))

或者,您应该可以使用 csv.reader