1

So I have data that looks something like this:

>> print data
"12345","string1","string2","string3","string4","string5"
"67890","string6","string7","string8","string9","string10"

I want to be able to put each line as an array, into an array. So in the end, I want it to look something like this:

>> print array_data
[["12345","string1","string2","string3","string4","string5"],
["67890","string6","string7","string8","string9","string10"]]

I have tried a few things and this is the closest I got:

>> temp_list = []
>> for line in data.splitlines(): temp_list.append([line])
>> print temp_list
[['"12345","string1","string2","string3","string4","string5"'],
['"67890","string6","string7","string8","string9","string10"']]

I am getting that single quote around each array. How should I proceed to get the result that I am looking for?

Thanks in advance!

1

5 Answers 5

1

You can use ast and split

setup

import ast
d = '''
"12345","string1","string2","string3","string4","string5"
"67890","string6","string7","string8","string9","string10"
'''
templist=[]

Then just

for line in d.strip().splitlines(): 
    templist.append([ast.literal_eval(st) for st in line.split(',')])
    

Notice that you can also make a oneliner using list comprehension

templist = [[ast.literal_eval(st) for st in line.split(',')] for line in d.strip().splitlines()]
Sign up to request clarification or add additional context in comments.

Comments

1

Using pandas:

import pandas as pd

d = '''\
"12345","string1","string2","string3","string4","string5"
"67890","string6","string7","string8","string9","string10"'''

l = pd.read_csv(pd.compat.StringIO(d), header=None, dtype='object').values

And you have l:

[['12345', 'string1', 'string2', 'string3', 'string4', 'string5'],
 ['67890', 'string6', 'string7', 'string8', 'string9', 'string10']]

Comments

1

Using csv and io modules from the standard library:

import csv
from io import StringIO

d = '''"12345","string1","string2","string3","string4","string5"
"67890","string6","string7","string8","string9","string10"'''

res = list(csv.reader(StringIO(d)))

print(res)

[['12345', 'string1', 'string2', 'string3', 'string4', 'string5'],
 ['67890', 'string6', 'string7', 'string8', 'string9', 'string10']]

Comments

1

Given:

>>> data
'"12345","string1","string2","string3","string4","string5"\n"67890","string6","string7","string8","string9","string10"'

You can do (if the ' do not otherwise indicate csv fields that might have , in them!):

>>> [line.replace('"','').split(',') for line in data.splitlines()]
[['12345', 'string1', 'string2', 'string3', 'string4', 'string5'], ['67890', 'string6', 'string7', 'string8', 'string9', 'string10']]

Or you can use csv to properly handle commas in the fields:

>>> import csv
>>> [list(e) for e in csv.reader(data.splitlines())]
[['12345', 'string1', 'string2', 'string3', 'string4', 'string5'], ['67890', 'string6', 'string7', 'string8', 'string9', 'string10']]

Comments

-1

You are almost there.

>> temp_list = []
>> temp_list = [list(eval(line)) for line in data.splitlines()]
>> print temp_list
[['12345', 'string1', 'string2', 'string3', 'string4', 'string5'], ['67890', 'string6', 'string7', 'string8', 'string9', 'string10']]

1 Comment

If you downvote a answer, also provide a reason for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.