把 rule name 作为表头,hrp-to-local作为数据保存到excel中
时间: 2024-04-09 08:33:44 浏览: 25
你可以使用`pandas`库来处理数据并将其保存到Excel文件中。以下是一个示例代码:
```python
import pandas as pd
def extract_data(file_path):
# 读取txt文件内容
with open(file_path, 'r') as file:
content = file.read()
# 使用正则表达式提取rule name和对应的数据
pattern = r'rule name (.*?)\n(.*?)\n'
matches = re.findall(pattern, content, re.DOTALL)
# 创建一个字典来保存数据
data = {}
for match in matches:
rule_name = match[0]
rule_data = match[1].strip()
data[rule_name] = rule_data
return data
def save_to_excel(data, output_file):
# 创建一个DataFrame对象
df = pd.DataFrame(data.items(), columns=['rule name', 'data'])
# 保存DataFrame到Excel文件
df.to_excel(output_file, index=False)
# 示例用法
file_path = 'example.txt' # 替换为你的txt文件路径
output_file = 'output.xlsx' # 替换为你想要保存的Excel文件路径
data = extract_data(file_path)
save_to_excel(data, output_file)
```
这段代码会读取`example.txt`文件中的内容,并提取`rule name`和对应的数据。然后,它将数据保存到一个DataFrame对象中,并将DataFrame保存到`output.xlsx`文件中。你可以根据实际情况修改文件路径和正则表达式的模式。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)