简体   繁体   English

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

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

I'm looking for help, I have multipath output from centos server in a .txt file, which looks like this. 我正在寻找帮助,我从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

It should look like this when exported to a .csv file. 导出到.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

This is as far i got, but this just reads every line and puts it into a different column every time it finds a space 这是我所知道的,但是它只会读取每一行,并在每次找到空格时将其放入不同的列中

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())

Assuming that you only have the two types of entry as described in your above sample, you can define each line as a factor of the number of elements within it that will be seperated by line.split() . 假设您只有上述示例中所述的两种类型的条目,则可以将每行定义为其中将由line.split()分隔的元素数量的一个因素。 For example: 例如:

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

Of course, using a regex to work if the line contains the type of data that you need, rather than just the right number of items, will be more flexible. 当然,如果该行包含所需的数据类型而不是仅包含正确数量的项目,则使用正则表达式将更灵活。 But this should get you started. 但这应该可以帮助您入门。

By processing the lines in this order, you'll always pick up a new disk_name / lun , and then assign the following "data" lines to that disk. 通过按此顺序处理行,您将始终选择一个新的disk_name / lun ,然后将以下“数据”行分配给该磁盘。 When you hit a new disk, the lines following that will be associated with the new disk, etc. 当您命中新磁盘时,其后的行将与新磁盘相关联,依此类推。

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

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