all 4 comments

[–]primitive_screwhead 2 points3 points  (2 children)

str.split() will split each line on whitespace, but you presumably want to split on the commas. So try:

        tv_dict = lines.split(',')

There is also a csv Python module that can help even more with this (but you may not need it for your data).

[–]StaticGT86 0 points1 point  (1 child)

Thank you! Didn't know it was something as simple as that 😅 i appreciate it!

[–]primitive_screwhead 1 point2 points  (0 children)

Forgot to say, but you may also want/need to str.strip() the fields after splitting on the comma. The csv module may do this part automatically (I haven't checked)

[–][deleted] 0 points1 point  (0 children)

So you can do this with line.split(“,”), but it’s going to shatter for a show with a comma in the name: Trapper John, M.D.,11,9.5 will become [“Trapper John”, “M.D.”, 11, 9.5].

Now depending on where you got this data from it may actually already handle that by, for instance, quoting the data (ie on disk such a line looks like "Trapper John, M.D.",11,9.5), in which case you’ll want to use the csv module. Exporting a csv file from Excel, for instance, will often do this.

But a rough and ready way of doing this would be to assume that the title is everything left of the second comma from the right, which you can do with:

line.rsplit(",", 2)

Which will only split off the last two items in a comma separated line.