112
112
from larray .utils import (table2str , size2str , unique , csv_open , unzip , long ,
113
113
decode , basestring , unicode , bytes , izip , rproduct ,
114
114
ReprString , duplicates , array_lookup2 , strip_rows ,
115
- skip_comment_cells , find_closing_chr , PY3 )
115
+ skip_comment_cells , find_closing_chr , StringIO , PY3 )
116
+
116
117
117
118
def _range_to_slice (seq , length = None ):
118
119
"""
@@ -9165,7 +9166,7 @@ def from_lists(data, nb_index=None, index_col=None):
9165
9166
return df_aslarray (df , raw = index_col is None , parse_header = False )
9166
9167
9167
9168
9168
- def from_string (s , nb_index = None , index_col = None , sep = ',' ):
9169
+ def from_string (s , nb_index = None , index_col = None , sep = ',' , ** kwargs ):
9169
9170
"""Create an array from a multi-line string.
9170
9171
9171
9172
Parameters
@@ -9179,14 +9180,18 @@ def from_string(s, nb_index=None, index_col=None, sep=','):
9179
9180
List of columns for the index (ex. [0, 1, 2, 3]). Defaults to None (see nb_index above).
9180
9181
sep : str
9181
9182
delimiter used to split each line into cells.
9183
+ \**kwargs
9184
+ See arguments of Pandas read_csv function.
9182
9185
9183
9186
Returns
9184
9187
-------
9185
9188
LArray
9186
9189
9187
9190
Examples
9188
9191
--------
9189
-
9192
+ >>> from_string("sex,M,F\\ n,0,1")
9193
+ sex | M | F
9194
+ | 0 | 1
9190
9195
>>> from_string("nat\\ sex,M,F\\ nBE,0,1\\ nFO,2,3")
9191
9196
nat\sex | M | F
9192
9197
BE | 0 | 1
@@ -9200,6 +9205,16 @@ def from_string(s, nb_index=None, index_col=None, sep=','):
9200
9205
nat\sex | M | F
9201
9206
BE | 0 | 1
9202
9207
FO | 2 | 3
9208
+ >>> from_string('''age,nat\\ sex, M, F
9209
+ ... 0, BE, 0, 1
9210
+ ... 0, FO, 2, 3
9211
+ ... 1, BE, 4, 5
9212
+ ... 1, FO, 6, 7''')
9213
+ age | nat\sex | M | F
9214
+ 0 | BE | 0 | 1
9215
+ 0 | FO | 2 | 3
9216
+ 1 | BE | 4 | 5
9217
+ 1 | FO | 6 | 7
9203
9218
9204
9219
Empty lines at the beginning or end are ignored, so one can also format the string like this:
9205
9220
@@ -9212,9 +9227,8 @@ def from_string(s, nb_index=None, index_col=None, sep=','):
9212
9227
BE | 0 | 1
9213
9228
FO | 2 | 3
9214
9229
"""
9215
- data = [[cell .strip () for cell in line .split (sep )]
9216
- for line in s .strip ().splitlines ()]
9217
- return from_lists (data , nb_index = nb_index , index_col = index_col )
9230
+
9231
+ return read_csv (StringIO (s ), nb_index = nb_index , index_col = index_col , sep = sep , skipinitialspace = True , ** kwargs )
9218
9232
9219
9233
9220
9234
def read_csv (filepath , nb_index = None , index_col = None , sep = ',' , headersep = None , na = np .nan ,
0 commit comments