我对 Python 很陌生,希望用它来解析文本文件。该文件包含 250-300 行,格式如下:
---- Mark Grey ([email protected] /cdn-cgi/l/email-protection) changed status from Busy to Available @ 14/07/2010 16:32:36 ----
---- Silvia Pablo ([email protected] /cdn-cgi/l/email-protection) became Available @ 14/07/2010 16:32:39 ----
我需要将此文件中的所有条目的以下信息存储到另一个文件(excel 或文本)中
UserName/ID Previous Status New Status Date Time
因此,对于上述条目,我的结果文件应如下所示
Mark Grey/[email protected] /cdn-cgi/l/email-protection Busy Available 14/07/2010 16:32:36
Silvia Pablo/[email protected] /cdn-cgi/l/email-protection NaN Available 14/07/2010 16:32:39
提前致谢,
任何帮助将非常感激
让您开始:
result = []
regex = re.compile(
r"""^-*\s+
(?P<name>.*?)\s+
\((?P<email>.*?)\)\s+
(?:changed\s+status\s+from\s+(?P<previous>.*?)\s+to|became)\s+
(?P<new>.*?)\s+@\s+
(?P<date>\S+)\s+
(?P<time>\S+)\s+
-*$""", re.VERBOSE)
with open("inputfile") as f:
for line in f:
match = regex.match(line)
if match:
result.append([
match.group("name"),
match.group("email"),
match.group("previous")
# etc.
])
else:
# Match attempt failed
会给你一个比赛部分的数组。然后我建议你使用csv module http://docs.python.org/library/csv.html以标准格式存储结果。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)