from enum import Enum class ParserState(Enum): STARTING_VALUE = 0 READING_VALUE = 1 ESCAPING = 2 CLOSING_ESCAPE = 3 def split_csv_row(raw_data, separator=",", escape='"'): state: ParserState = ParserState.STARTING_VALUE arr = [] current_item = "" for c in raw_data: if state == ParserState.STARTING_VALUE: if c == escape: state = ParserState.ESCAPING elif c == separator: arr.append("") else: state = ParserState.READING_VALUE current_item = c elif state == ParserState.READING_VALUE: if c == separator: state = ParserState.STARTING_VALUE arr.append(current_item) current_item = "" else: current_item += c elif state == ParserState.ESCAPING: if c == escape: state = ParserState.CLOSING_ESCAPE else: current_item += c elif state == ParserState.CLOSING_ESCAPE: if c == escape: state = ParserState.ESCAPING current_item += c else: state = ParserState.READING_VALUE arr.append(current_item) current_item = "" arr.append(current_item) return arr if __name__ == "__main__": test_1 = 'Rangement bouffe final,"Ranger la nourriture, gérer les restes, ranger les tupperware",Anne-Gaëlle,Anne-Gaëlle,,,' print(split_csv_row(test_1)) test_1 = 'Installation prépa public,Install / prépa public,","",", coucou' print(split_csv_row(test_1)) test_3 = ', prépa public,","",""", coucou,' print(split_csv_row(test_3))