用python 脚本,在 ------------------------------------------------------------------------ r3 | svn_0101 | 2023-06-10 18:14:27 +0800 (周六, 10 六月 2023) | 1 line Changed paths: A /branches/test.txt --id=1235---user=李四【版本跟过过过过】急急急急急军军军军牛牛牛牛牛11122------------------------------------------------------------------------ r6 | svn_0101 | 2023-06-11 17:06:23 +0800 (周日, 11 六月 2023) | 1 line Changed paths: A /branches/4.txt A /branches/6.txt --id=1235---user=张三【版本跟过过过过】急急急急急军军军军牛牛牛牛牛11122 提取时间和姓名,并导出到表格中,表格以时间和姓名作为表头
时间: 2023-08-15 20:42:24 浏览: 42
以下是提取信息并导出到表格中的 Python 脚本:
```python
import re
import csv
# 从文本中提取时间和姓名
def extract_info(text):
pattern = r'^\s*-{72}\n\s*r\d+\s*\|\s*\S+\s*\|\s*(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}\s+\+\d{4})\s+\(.*?\)\s+\|\s+\d+\s+line\s+Changed\s+paths:\s+(.*?)\s+--id=\d+---user=(.*?)【版本跟.*$'
match = re.match(pattern, text, re.DOTALL)
if match:
return match.group(1, 3)
# 读取文本文件
with open('svn_log.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 提取每个记录的时间和姓名
records = []
for match in re.finditer(r'^\s*-{72}\n', text, re.MULTILINE):
record_text = text[match.start():]
next_match = re.search(r'^\s*-{72}\n', record_text[72:], re.MULTILINE)
if next_match:
record_text = record_text[:72+next_match.start()]
info = extract_info(record_text)
if info:
records.append(info)
# 导出到表格
with open('records.csv', 'w', encoding='utf-8', newline='') as f:
writer = csv.writer(f)
writer.writerow(['时间', '姓名'])
writer.writerows(records)
```
将以上代码保存为 `extract_info.py` 文件并与 `svn_log.txt` 文件放在同一目录下,然后在命令行中执行以下命令:
```
python extract_info.py
```
这样就可以生成 `records.csv` 文件,其中包含提取的时间和姓名信息。