Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I have string final_line as

0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0130631958731,0.0,0.0,0.0,0.0,0.0,0.0,0.0130631958731,0.0,0.0,0.0,0.0,0.0,0.0,,
0.00507937313707,0.0,0.0,0.0201058520009,0.0,0.0,0.0459562331449,0.0268078026679,0.0,0.0103772139359,0.0,0.0,0.0438673134565,0.0,0.0268078026679,0.0,0.0,0.0,0.0,0.0,0.0224437417685,,
0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.070802847389,0.0,0.0,,
0.0,0.0,0.169140135429,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,,
0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0261263917462,0.0,0.0,0.0,0.0,0.0,0.0,0.0261263917462,0.0,0.0,0.0,0.0,0.0,0.0,,
0.0961428138228,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,,

I want to convert it into numpy array for without transferring to CSV file. Here each line in string should be converted to different row as

[['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0.08364751'], ['0.017944717', '0', '0', '0', '0.009470823', '0', '0', '0', '0', '0', '0'], ['0.012620501', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0.012620501', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0.01332164', '0', '0', '0', '0', '0', '0'], ['0.012620501', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0.097414177', '0.042092545', '0', '0', '0', '0'], ['0', '0', '0', '0', '0.01332164', '0', '0', '0', '0', '0', '0'], ['0.005324215', '0', '0', '0.04598186', '0.028100025', '0', '0', '0', '0', '0', '0.023525603'], ['0', '0', '0', '0', '0', '0', '0', '0', '0.055765006', '0', '0'], ['0', '0', '0.133216404', '0', '0', '0', '0', '0', '0', '0', '0']]

I know here output having different values but I have just shown to get the format

Example

I have string as

a1,b1,c1,d1,e1,,
a2,b2,c2,d2,e2,,
a3,b3,c3,d3,e3,,

then output should be

[['a1','b1','c1','d1','e1'],
 ['a2','b2','c2','d2','e2'],
 ['a3','b3','c3','d3','e3']]

I have seen other questions but it converts into only single row of array. Here I want every single line in new row as shown in example.

share|improve this question

marked as duplicate by askewchan, Code Lღver, mu 無, John Palmer, Kumar KL Apr 22 at 7:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4  
1  
Add a reshape to the answer @Robert linked to get a 2D array. For example, np.fromstring(final_line, sep=',').reshape(final_line.count(',,')+1, -1) –  askewchan Apr 21 at 21:26
add comment

2 Answers 2

up vote 0 down vote accepted

map can be useful in this case. Also, the last ,, in your final list is not necessary so I just ignored it (look at :-2 before split operation).

import numpy as np

A = final_line[:-2].split(',,')
B = np.array([map(float,a.split(',')) for a in A])
share|improve this answer
    
It gives me ValueError: could not convert string to float: –  DummyGuy Apr 22 at 9:09
    
I did exceptional handling. It gives me error on last line of data. It may contain some invisible data because it did problem with csv file too. –  DummyGuy Apr 22 at 9:14
    
yes it gives you the error if your file is not properly formatted –  ysakamoto Apr 22 at 17:46
    
actually :-2 removes my invisible data too. thanks. –  DummyGuy Apr 22 at 21:24
add comment

Your data is in the form of lists, with each line separated by an additional comma.

d_in = "1,2,3,,4,5,6,,7,8,9"
first = d_in.split(",,")
final = [line.split(",") for line in first]

first stores a list of strings (each of your columns). final stores your data in the proper format (in my example, [[1,2,3],[4,5,6],[7,8,9]])

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.