Hello,
Programming/Python newb here looking for some help with arrays. I am trying to write a parsing program that takes a comma delimited csv file, compares entries, and outputs the comparisons in a particular format.
The csv file has two columns. Col [0] contains article identifiers, col[1] contains assigned keywords. It looks something like this:
00002944423, antibiotics
00002944423, resistance
00002953423, mortality
I wrote the following code, which compares the entry in row i, col 0, with the entry in row i+1, col 0. If the article id's are equal, then I need to print out the two keywords on the same line.
import csv
edgelist1=[] #initialize the first edge list
listmax = 0 #use to calculate the largest index value of the array
#import the article_kwd file
reader = csv.reader(open('C:/scientometric/kw.csv', 'r'), delimiter = ',', quoting=csv.QUOTE_NONE)
for row in reader:
edgelist1.append (row)
listmax = listmax+1
#use to determine whether or not two keywords are connected
#by checking to see if they are both assigned to the same article
for i in range (0,listmax):
nextrow=i+1
while edgelist1[i][0]==edgelist1[nextrow][0]:
print (edgelist1[i][1],edgelist1[nextrow][1])
nextrow=nextrow+1
This works really well. It produces an output like this:
ANTIMICROBIAL RESISTANCE CARRIAGE
ANTIMICROBIAL RESISTANCE EXPERIENCE
ANTIMICROBIAL RESISTANCE INFECTION
ANTIMICROBIAL RESISTANCE PNEUMOCOCCAL-INTERVENTION-PROJECT
ANTIMICROBIAL RESISTANCE SEROTYPES
ANTIMICROBIAL RESISTANCE SPAIN
ANTIMICROBIAL RESISTANCE STRAINS
However, I need to take this one step further. I need to replace the keywords with their key values. The output needs to look something like this:
1 2
1 3
1 4
2 3
What I have been trying to do is write the output into an interim array. However, no matter what I try, I keep getting an error message that the list index is out of range.
Any advice on how to get past this?