繁体   English   中英

读取.txt文件并将选择性数据导出到.csv

[英]Read .txt file and export selective data to .csv

我正在寻找帮助,我从centos服务器在.txt文件中输出了多路径,如下所示。

asm (393040300403de) dm-12 HITACHI
size=35G queue_if_no_path
  |- 1:0:0:18  sda  65:48   active ready running
  `- 3:0:0:18  sdbc 70:368  active ready running
3600300300a4c dm-120 HITACHI
size=50G queue_if_no_path
  |- 1:0:0:98  sdc 70:48   active ready running
  `- 3:0:0:98  sdca 131:368 active ready running

导出到.csv文件时,它应该看起来像这样。

DISKS_NAME  LUN             LUNID DM-NAME SIZE  MULTPATH
asm       393040300403de    03de  dm-12    35G  sda  sdbc
No_device  3600300300a4c    0a4c  dm-120   50G  sdc  sdca

这是我所知道的,但是它只会读取每一行,并在每次找到空格时将其放入不同的列中

import csv

readfile = 'multipath.txt'
writefile = 'data.csv'
with open(readfile,'r') as a, open(writefile, 'w') as b:
    o=csv.writer(b)
    for line in a:
        o.writerow(line.split())

假设您只有上述示例中所述的两种类型的条目,则可以将每行定义为其中将由line.split()分隔的元素数量的一个因素。 例如:

disk_name = ""
... # other parameters you need to keep track of across lines. I'd suggest creating a class for each lun/disk_name.

for line in a:
    line_data = line.split()

    if len(line_data) == 4:
        # this will match and 'asm (393040300403de) dm-12 HITACHI'
        disk_name, lun, dm_name, _ = line_data
        # process these variables accordingly (instantiate a new class member)
        continue # to read the next line

    else if len(line_data) == 3:
        # this will match '3600300300a4c dm-120 HITACHI'
        lun, dm_name, _ = line_data
        disk_name = "No_device"
        # process these variables accordingly
        continue

    if len(line_data) == 2:
        # this will match 'size=35G queue_if_no_path'
        size, _ = line_data
        # process the size accordingly, associate with the disk_name from earlier
        continue

    if len(line_data) == 7:
        # this will match '|- 1:0:0:18  sda  65:48   active ready running' etc.
        _, _, path, _, _, _, _ = line_data
        # process the path accordingly, associate with the disk_name from earlier
        continue

当然,如果该行包含所需的数据类型而不是仅包含正确数量的项目,则使用正则表达式将更灵活。 但这应该可以帮助您入门。

通过按此顺序处理行,您将始终选择一个新的disk_name / lun ,然后将以下“数据”行分配给该磁盘。 当您命中新磁盘时,其后的行将与新磁盘相关联,依此类推。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM