-1

Im reading data from a csv file, here a snippet of the output.

['movie_title', 'director_name', 'duration', 'gross', 'genres', 'actor_1_name', 'actor_2_name', 'actor_3_name', 'facenumber_in_poster', 'plot_keywords', 'language', 'country', 'content_rating', 'budget', 'title_year', 'imdb_score', 'movie_facebook_likes,AvatarÂ\xa0', 'James Cameron', '178', '760505847', 'Action|Adventure|Fantasy|Sci-Fi', 'CCH Pounder', 'Joel David Moore', 'Wes Studi']

There is the '\n' that im trying to get rid of and the 'Â\xa0' in every one of the movie titles, heres what ive tried with the failed output.

split2 = [elem.replace('\n', ',') for elem in split2]

with output 'movie_facebook_likes,AvatarÂ\xa0', which successfully gets rid of \n but now the list qoutations are messed up.

split2 = [elem.replace('\n', "','") for elem in split2]

with output "movie_facebook_likes','AvatarÂ\xa0",

fixes the last problem but now the wrong type of quotations are there, does anyone have a suggestion, im honestly stumped. Im just trying to get \n out of the way then i can work on Â\xa0 later.

Salio
  • 1,058
  • 10
  • 21
shwirms
  • 11
  • 1
  • See https://stackoverflow.com/questions/10993612/how-to-remove-xa0-from-string-in-python – Dhruv Mar 13 '22 at 17:33
  • I don't see any '\n' in there. And why are you trying to fix the bad list with additional code instead of fixing the code that produced the bad list? – Kelly Bundy Mar 13 '22 at 17:36
  • Because this is a csv file that our prof gave us with 5000+ lines just like this. I cant mutate the csv file as he will take marks off. I gave the wrong output code regarding the \n, the original output as follows: '6.8', '85000\nThe Dark Knight RisesÂ\xa0', 'Christopher Nolan', – shwirms Mar 13 '22 at 21:53
  • So the prof gave you a csv file. Not a Python list. So there can still be a flaw in whatever code you wrote to turn the csv file into a Python list. We can't tell because you neither showed the file nor that code. Also, if it *is* a problem not in your code but in the csv file, then, unless the point of the exercise is to handle the badness of the file, I'd *tell* the prof about the badness so that *they* can fix it. – Kelly Bundy Mar 13 '22 at 22:46
  • I mean the first sentence of the post was that I was reading a csv file. Thanks. – shwirms Mar 14 '22 at 16:20
  • And your point is? – Kelly Bundy Mar 14 '22 at 16:23
  • aNd YoUr pOiNt iS – shwirms Mar 15 '22 at 19:04

1 Answers1

-1

Your code makes no sense at all. However, try this out:

splitted_list = [s.replace('\n','').replace('AvatarÂ\xa0,'') for s in <whatever it is>]

This code basically replaces both '\n' and the other Avatar thing with ''. This will give you your desired output. Tip: Don't spam variables with same names, especially for .replace()

The Myth
  • 1,090
  • 1
  • 4
  • 16