Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将正则表达式的结果保存到csv或xls_Python - Fatal编程技术网

Python 将正则表达式的结果保存到csv或xls

Python 将正则表达式的结果保存到csv或xls,python,Python,我有这样的日志记录(数百万行): 根据@peter wood的评论,不清楚您的输入是什么。然而,假设您的输入是您发布的,那么这里有一个最小的解决方案,可以在给定的结构下工作。如果不完全正确,您应该能够轻松地将其更改为搜索真正属于您的结构的内容 import csv # You need to change this path. lines = [row.strip() for row in open('/path/to/log.txt').readlines()] # You need to

我有这样的日志记录(数百万行):


根据@peter wood的评论,不清楚您的输入是什么。然而,假设您的输入是您发布的,那么这里有一个最小的解决方案,可以在给定的结构下工作。如果不完全正确,您应该能够轻松地将其更改为搜索真正属于您的结构的内容

import csv

# You need to change this path. 
lines = [row.strip() for row in open('/path/to/log.txt').readlines()]
# You need to change this path to where you want to write the file.
with open('/path/to/write/to/mydata.csv', 'w') as fh:
    # If you want a different delimiter, like tabs '\t', change it here.
    writer = csv.writer(fh, delimiter=',')
    for l in lines:
        # You can cut and paste the tokens that start and stop the pieces you are looking for here.
        service = l[l.find('previous_status>')+len('previous_status>'):l.find('</previous_status')]
        sensor = l[l.find('device_id>')+len('device_id>'):l.find('</device_id>')]
        device = l[l.find('<DEVICE>')+len('<DEVICE>'):l.find('</device_type>')]
        writer.writerow([service, sensor, device])
导入csv
#您需要更改此路径。
lines=[row.strip()表示打开('/path/to/log.txt')中的行。readlines()]
#您需要将此路径更改为要写入文件的位置。
将open('/path/to/write/to/mydata.csv',w')作为fh:
#如果需要不同的分隔符,如制表符“\t”,请在此处更改。
writer=csv.writer(fh,分隔符=',')
对于l in行:
#你可以剪切和粘贴开始和停止你在这里寻找的片段的标记。
service=l[l.find('previous_status>')+len('previous_status>):l.find('')+len('device_id>'):l.find('')
device=l[l.find(“”)+len(“”):l.find(“”)]
writer.writerow([服务、传感器、设备])

传感器和设备从哪里来?啊,我明白了,正确格式化后,你确定这不是xml吗?它看起来是畸形的<代码>服务和
传感器
是元素内容,而
设备
是标签。
ISCS
OK
会发生什么情况?它们是大写的,应该包括在内吗?如果不是,我们怎么知道忽略它们呢?谢谢肖恩和彼得。谢谢你在这个论坛上回答我的第一个问题。这很有帮助。我在这件事上陷入了困境。我从未处理过日志数据。我的python仅限于使用库。Shawn有什么资源可以帮助我学习更多关于xml正则表达式的知识吗?要解析xml,可以使用python世界中最受欢迎的BeautifulSoup或lxml。但是您应该能够使用提供的代码立即得到结果。对你有用吗?如果是这样,请接受并关闭它。祝你好运
SERVICE SENSORS DEVICE
import csv

# You need to change this path. 
lines = [row.strip() for row in open('/path/to/log.txt').readlines()]
# You need to change this path to where you want to write the file.
with open('/path/to/write/to/mydata.csv', 'w') as fh:
    # If you want a different delimiter, like tabs '\t', change it here.
    writer = csv.writer(fh, delimiter=',')
    for l in lines:
        # You can cut and paste the tokens that start and stop the pieces you are looking for here.
        service = l[l.find('previous_status>')+len('previous_status>'):l.find('</previous_status')]
        sensor = l[l.find('device_id>')+len('device_id>'):l.find('</device_id>')]
        device = l[l.find('<DEVICE>')+len('<DEVICE>'):l.find('</device_type>')]
        writer.writerow([service, sensor, device])