D
D
denislysenko2021-11-24 19:30:51
Python
denislysenko, 2021-11-24 19:30:51

How to split strings by commas when there are commas in some values ​​that do not need to be split?

there is a movies.csv file that
looks like this:

movieId,title,genres
1,Toy Story (1995),Adventure|Animation|Children|Comedy|Fantasy
2,Jumanji (1995),Adventure|Children|Fantasy
3,"American President, The (1995)",Comedy|Drama|Romance
4,Waiting to Exhale (1995),Comedy|Drama|Romance
5,Father of the Bride Part II (1995),Comedy
6,"City of Lost Children, The (Cité des enfants perdus, La) (1995)",Adventure|Drama|Fantasy|Mystery|Sci-Fi
7,"Postman, The (Postino, Il) (1994)",Comedy|Drama|Romance
8,"Indian in the Cupboard, The (1995)",Adventure|Children|Fantasy
9,Tom and Huck (1995),Adventure|Children
...

running the program should look like this:
cat movies.csv | python3 program.py

i do splits like this

for line in sys.stdin:
    list = line.split(',')

that is, I do a comma split, but we have lines in this file that already have a comma in the title,
for example:
3,"American President, The (1995)",Comedy|Drama|Romance
if I do, I apply a comma split to this line, then I have the following result:
['3', '"American President', ' The (1995)"', 'Comedy|Drama|Romance']
and I need the result to be like this:
['3', ' "American President, The (1995)"', 'Comedy|Drama|Romance']

how do I read lines from sys.stdin to split them correctly into id, title and genre even if there is a comma in the title?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-11-24
@denislysenko

Split with regex or use csv module.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question