Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
How to convert the below line of string:

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