Home > Software engineering >  How can I modify this code to print details of the variables for each element/row within the csv fil
How can I modify this code to print details of the variables for each element/row within the csv fil

Time:08-25

I have a csv file containing rows of information. and I have this code, which prints what I want it to print, but I am trying to get it print all rows as opposed to the once specified. I understand I'll need a for loop of some sort, but I am not 100% sure where it should go.

I simply want this to print all the items in list/file, so they can all be seen.

columns = defaultdict(list)
with open('scanresults.csv') as f:
    reader = csv.DictReader(f)
    for row in reader:
        for(k, v) in row.items():
            columns[k].append(v)
    qid = (columns['QID'][170])
    title = (columns['Title'][170])
    severity = (columns['Severity'][170])
    threat = (columns['Threat'][170])
    impact = (columns['Impact'][170])
    solution = (columns['Solution'][170])

print('QID: ', qid)
print('Title: ',  title)
print('Severity: ',  severity)
print('Threat: ', threat)
print('Impact: ', impact)
print('Solution: ',  solution)
print('--------------------------------------------------------------------------------------')
#the next row/items[enter image description here][1] will be printed after this line.

#outcome

#QID:  121993
#Title:  CentOS Security Update for Kernel (CESA-2014:0328)
#Severity:  4
#Threat:  CentOS has released security update for kernel to fix vulnerabilities. Affected Products: CentOS 6
#Impact:  Successful exploitation allows attacker to compromise the system.
Solution:  To resolve this issue, upgrade to the latest packages which contain a patch. Refer to CentOS advisory centos 6 (http://lists.centos.org/pipermail/centos-announce/2014-March/020230.html) for updates and patch information.

#expected outcome

#QID:  121993
#Title:  CentOS Security Update for Kernel (CESA-2014:0301)
#Severity:  4
#Threat:  CentOS has released security update for kernel to fix vulnerabilities. Affected Products: CentOS 6
#Impact:  Successful exploitation allows attacker to compromise the system.
Solution:  To resolve this issue, upgrade to the latest packages which contain a patch. Refer to CentOS advisory centos 6 (http://lists.centos.org/pipermail/centos-announce/2014-March/020230.html) for updates and patch information.
-------------------------------------------------------------------------------------------
#QID:  121925
#Title:  CentOS Security Update for pain (CESA-2014:0375)
#Severity:  3
#Threat:  CentOS has released security update for kernel to fix vulnerabilities. Affected Products: CentOS 6
#Impact:  Successful exploitation allows attacker to compromise the system.
Solution:  To resolve this issue, upgrade to the latest packages which contain a patch. Refer to CentOS advisory centos 6 (http://lists.centos.org/pipermail/centos-announce/2014-March/020230.html) for updates and patch information.
-------------------------------------------------------------------------------------------
#QID:  129843
#Title:  CentOS Security Update for pain (CESA-2014:0321)
#Severity:  5
#Threat:  CentOS has released security update for kernel to fix vulnerabilities. Affected Products: CentOS 6
#Impact:  Successful exploitation allows attacker to compromise the system.
Solution:  To resolve this issue, upgrade to the latest packages which contain a patch. Refer to CentOS advisory centos 6 (http://lists.centos.org/pipermail/centos-announce/2014-March/020230.html) for updates and patch information.


  [1]: https://i.stack.imgur.com/fVZe5.png

CodePudding user response:

Currently you are getting the data from the last row, and then printing that. You want to print the data for every row, like this:

columns = defaultdict(list)
with open('scanresults.csv') as f:
    reader = csv.DictReader(f)
    rowcount = 0
    for row in reader:
        for(k, v) in row.items():
            columns[k].append(v)
        print('QID: ', columns['QID'][rowcount])
        print('Title: ',  columns['Title'][rowcount])
        print('Severity: ',  columns['Severity'][rowcount])
        print('Threat: ', columns['Threat'][rowcount])
        print('Impact: ', columns['Impact'][rowcount])
        print('Solution: ',  columns['Solution'][rowcount])
        print('--------------------------------------------------------------------------------------')
        rowcount  = 1

EDIT - This version is more concise: (but it doesnt use defaultdict)

with open('scanresults.csv') as f:
    reader = csv.DictReader(f)
    for row in reader:
        print('QID: ', row['QID'])
        print('Title: ', row['Title'])
        print('Severity: ', row['Severity'])
        print('Threat: ', row['Threat'])
        print('Impact: ', row['Impact'])
        print('Solution: ', row['Solution'])
        print('--------------------------------------------------------------------------------------')
  • Related