0

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)
7
  • 1
    str.split() returns a list, so split_col would be a list. Commented Nov 19, 2015 at 23:40
  • if the file does indeed look like that, it looks like split_col[0] is the ISBN and split_col[1] is the book title, I dont think you need your second split Commented Nov 19, 2015 at 23:42
  • In your example, ISBN and title are not separated by a comma. Notice also that you print after exiting the for loop so you only get the final values. Commented Nov 19, 2015 at 23:44
  • okay. we got it. whoops Commented Nov 19, 2015 at 23:46
  • 2
    In your for loop, busers[ISBN] = title Commented Nov 19, 2015 at 23:55

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.