【发布时间】:2023-03-16 03:15:01
【问题描述】:
我正在尝试解析具有多个键、值行的文件,如下所示
"key1" = "value1";
"key2" = "value2";
"key3" = "value3_line1
value3_line2
value3_line3";
"key4" = "value4";
我正在使用下面的代码来解析这个文件
def parseFile(f):
regex = re.compile(r'^"(.*)"\s+=\s+"(.*)";',re.MULTILINE)
with open(f) as string_file:
alllines = string_file.read()
matches = [m.groups() for m in regex.finditer(alllines)]
for m in matches:
print(m[0], '=>', m[1])
此代码匹配具有 key1、key2 和 key4 但不匹配 key3 的行。如何解决此问题以获取所有键值对,包括具有多行值的键值对?
【问题讨论】:
-
regex = re.compile(r'^"(.*)"\s+=\s+"(.*)"?;?',re.MULTILINE)?