mmb_rach
mmb_rach

Reputation: 163

reading csv file, can't split values because of list error-python

I am reading in a file. It is information about various books, and very large. I just need the ISBN and the title of the book.

After separating by a semi-colon, the ISBN and book title are separated by a comma. I figured all I would have to do is separate by the semi-col, then separate by the comma, and then just get the first item[0] to return the ISBN and then get the second item[1] to return the title.

Getting an error saying that "list object has no attribute split". I am confused by this, it seems that python is interpreting the variable split_col as a list, yet I never define it as one.

Even weirder to me, when I run the file, even though it is in the for loop, it is only returning one line of information from the file. It's not the first line of the file either and it returns as a list.

The file looks something like this: 0195153448;Classical Mythology;Mark P. O. Morford;2002;Oxford University Press;http://images.amazon.com/images/P/0195153448.01.THUMBZZZ.jpg;http://images.amazon.com/images/P/0195153448.01.MZZZZZZZ.jpg;http://images.amazon.com/images/P/0195153448.01.LZZZZZZZ.jpg,,,,,,,,,,,,,,,,,,,,,

All I need is the first two items, the ISBN and the title.

What is going on here? All of the print statements were just there for me to test what was going on, yet I can't even see these values because of the error I am getting. I am just getting started reading this file. Eventually, I need to use the data from this file and the data from another file to make a dictionary. But for right now I'm just trying to figure out what is going wrong with this read code.

busers = {}
books = []
infile3 = open("BBooks.csv","r")
for line in infile3:
    split_col = line.split(";")
    split_comma = split_col.split(",")
    ISBN = split_comma[0]
    title = split_comma[1]
print(split_col)
print(split_comma)
print(ISBN)
print(title)

new code. problem is, it is still only printing one item from the very large file, and it is not the first item. It is however printing the ISBN and the title of this particular book using this form.

busers = {}
books = []
infile3 = open("BBooks.csv","r")
for line in infile3:
    split_col = line.split(";")
    ISBN = split_col[0]
    title = split_col[1]

print(ISBN)
print(title)

Upvotes: 0

Views: 153

Answers (1)

Noah
Noah

Reputation: 1731

If you wish to print out all ISBNs and titles, then you need to put the print statements into the for loop. It is only printing one book because you have the print statement after the for loop exits. It is printing the last book in your file.

The final statement shows you how to store the ISBNs and titles in the busers dictionary with the ISBN as key and title as value.

busers = {}
books = []
infile3 = open("BBooks.csv","r")
for line in infile3:
    split_col = line.split(";")
    ISBN = split_col[0]
    title = split_col[1]

    print(ISBN)
    print(title)

    busers[ISBN] = title

Upvotes: 2

Related Questions