How to convert the below line of string:
into:
As you may have noted you cannot use
#python #csv #reader #split #stringIO #byteIO
data = '15 0 42 50 "some text" "" 4 4 "text"'
into:
[15, 0, 42, 50, 'some text', '', 4, 4, 'text']
As you may have noted you cannot use
split
as there are texts with spaces in between. For that we can use the below code:import csv
import io
file = io.StringIO(data) # use io.BytesIO in python 2
reader = csv.reader(file, delimiter=' ')
split_data = next(reader)
parsed_data = [int(x) if x.isdigit() else x for x in split_data]
NOTE:
use io.BytesIO
instead of io.StringIO
in case you are using python 2.#python #csv #reader #split #stringIO #byteIO