简体   繁体   English

使用Python 2.7中的CSV lib对CSV文件中的数据进行排序

[英]Sort data in CSV file to which column it belongs to using CSV lib in Python 2.7

Previously I have opened this topic: Append continuous data to the same row using CSV lib, in Python 以前,我已经打开了这个主题: 在Python中使用CSV lib将连续数据追加到同一行

Which someone suggested a solution for me, that worked beautifully. 有人为我建议了一个解决方案,效果很好。 The code suggested that it worked is as follows: 该代码表明它的工作原理如下:

import csv

filewrite.writerow(['Hostname', 'Config-Error', 'Config-Error'])
with open(outputfile, 'a') as csvfile:
    filewrite = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
    row = [hostname]
    if (CiscoSyslog == 0):
        row.append('Syslog')
    if (CiscoSNMP == 0):
        row.append('SNMP')
    filewrite.writerow(row)

The output comes as desired: 输出随心所欲:

hostname,Config-Error,Config-Error
router1,Syslog
router2,SNMP
router3,Syslog,SNMP

Now, my next question is, how would you take that appended data to one row, and sort it to belong to a certain column? 现在,我的下一个问题是,您如何将追加的数据放入一行,然后将其排序为属于某一列? For example, instead of having two columns on top saying "Config-Error", I would like on one column at the top to say SyslogError, and the other SNMPError, so it would look something like: 例如,我不想在顶部的两列上说“ Config-Error”,而在顶部的一列上说SyslogError,而另一列是SNMPError,所以它看起来像:

hostname,SyslogError,SNMPError
router1,Syslog, ,
router2, ,SNMP
router3,Syslog,SNMP

My end goal is to match the error that will find in a configuration, append it to a CSV file, and sort it on the proper column. 我的最终目标是匹配将在配置中找到的错误,将其附加到CSV文件,然后在适当的列上对其进行排序。 I wasn't even sure how to "google search" this, so I decided to pose the question here. 我什至不知道如何“谷歌搜索”这个,所以我决定在这里提出这个问题。

I am still a little new to python, but learning has been fun so far. 我对python还是有点陌生​​,但是到目前为止学习仍然很有趣。

Thank you in advance! 先感谢您!

Viktor 维克多

The answer was so simple, it was "staring" in my eyes. 答案很简单,在我眼中是“凝视”。 :) :)

Basically, I got rid of the "if" statements, where data is appended under a row. 基本上,我摆脱了“ if”语句,该语句将数据附加在一行下。 I also included a little more of my code, so it fully makes sense, in case someone is looking for an answer to a similar puzzle. 我还添加了一些我的代码,因此,如果有人正在寻找类似难题的答案,这完全有意义。

So... I setup flags to begin with as "Yes" (as you can see in the code). 所以...我将标志设置为以“是”开头(如代码中所示)。 They all remain set to "Yes", should some of the statements for each variable (Syslog, SNMP and Tacacs) are not found. 如果找不到每个变量的某些语句(Syslog,SNMP和Tacacs),则它们都将设置为“是”。 I perform a "re.search" method, and if it finds the statement under a configuration, it flags it as "No", thus it does not need to be printed to the output file (or in other words, the configuration is present, so there is "No" config error). 我执行一个“ re.search”方法,如果它在配置下找到该语句,则将其标记为“ No”,因此不需要将其打印到输出文件中(换句话说,该配置已存在) ,因此没有“ No”配置错误)。 For any of the flags still remaining as "Yes", I print out each "flag" (variable in this case) in order , each time, that way it goes in the proper column . 对于仍然保留为“是”的任何标志,我每次都会按顺序打印出每个“标志”(在这种情况下是可变的),这样它将在正确的列中显示

Here is the code: 这是代码:

import csv
import re


Syslog = 'logging 1.1.1.1'
SNMP = 'snmp-server host 2.2.2.2'
Tacacs = 'tacacs-server host 3.3.3.3'

filewrite.writerow(['Hostname', 'SyslogError', 'SNMPError', 'TacacsError'])

with open(file, 'r') as inFile:
    SyslogFlag = 'Yes'
    SNMPFlag = 'Yes'
    TacacsFlag = 'Yes'

    for line in inFile:
        if re.search(Syslog, line):
            SyslogFlag = 'No'
        if re.search(SNMP, line):
            SNMPFlag = 'No'
        if re.search(Tacacs, line):
            TacacsFlag = 'No'

    if (SyslogFlag == 'No') and (SNMPFlag == 'No') and (TacacsFlag == 'No'):
        print "No Config errors"
    else:
        with open(outputfile, 'a') as csvfile:
            filewrite = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
            row = [hostname]
            row.append(SyslogFlag)
            row.append(SNMPFlag)
            row.append(TacacsFlag)
            filewrite.writerow(row)

Here is the output: 这是输出:

Hostname,SyslogError,SNMPError,TacacsError
router1,Yes,No,No
router2,No,Yes,No
router3,Yes,Yes,No

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

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