I have this code to check indexes in a file to see if they match, but to start off I am having trouble being able to select an index. What do I have to do in order to be able to do so, because at this moment it doesn’t show the values as being in a list.
def checkOS():
fid = open("C:/Python/NSRLOS.txt", 'r')
fhand = open("C:/Python/sha_sub_hashes.out", 'r')
sLine = fhand.readline()
line = fid.readline()
outdata = []
print line
checkOS()
Right now it prints:
"190","Windows 2000","2000","609"
I only want it to print: (so index[0])
190
And when I try index[0], I just get ' " '. So the first value in the whole string, I want a list to be able to select the index.
Try using
line.split(",")to split the line by the commas, then strip out the quotation marks by slicing the result.Example:
…and here’s the whole thing, abstracted into a function:
(This is assuming, of course, that all the items in the line are divided by commas, that they’re all wrapped by quotation marks, that there’s no spaces after the commas (although you could take care of that by doing
item.strip()to remove whitespace). It also fails if the quoted items contains commas, as noted in the comments.)