Converting a CSV list into an array - arrays

I have a variable that is a csv list and i am trying to convert it into an array using array_map but for some reason it keeps giving me the following error:
Warning: array_map(): Argument #2 should be an array in
$list1 = 1,7,15,16,18,18;
$shortArray = array_map('str_getcsv', $list1);
$var_dump($shortArray);
Does anyone know how to get this to work or is there another way to convert a csv list into an array?
I am using php 5.5.0.

$list1 = array(1,7,15,16,18,18);
$shortArray = array_map('str_getcsv', $list1);
var_dump($shortArray);

$list1 = "1,7,15,16,18,18"; // or the line from your CVS file
$shortArray = explode(",", $list1);
var_dump($shortArray);

Related

convert string array to float MEL

I having a trouble with a code in MAYA 2020.
I want to create a new expression into a default value of a Arnold aiUserDataInt
I need the default value of a aiUserDataInt = attribute from a geometry that I crate name "ID"
so, my code looks like this:
string $Selected[] = `ls -selection`;
for ($node in $Selected)
aiUserDataInt1.default = $Selected.id;
but I have these error:
// Error: Line 2.37: Cannot cast data of type string[] to float. //
So, I suppose default value do not accept arrays
my question would be: is there a way to convert array into float?
or what am I doing wrong?
Thanks in advance
Unfortunately that's not how mel works. You can do:
string $Selected[] = `ls -selection`;
for ($node in $Selected)
{
int $id = getAttr($node + ".id");
setAttr("aiUserDataInt1.default", $id);
}
Didn't test it, but it should work like this. You get and set attributes with getAttr() and setAttr().

How to create a loop to extract data from a .csv file

I face this issue and can't seem to find a fix except with Scipy or Numpy, both of which I don't wanna use in this case.
From a .csv file, I want to extract the values of the first column :
enter image description here
Which I manage to do with the following code :
mat_data = open('file.csv')
data_reader = csv.reader(mat_data)
list_data = list(data_reader)
value1=float(list_data[1][0])
value2=float(list_data[2][0])
value3=float(list_data[3][0])
I'd now like to create a loop that could be used and create value"i" no matter how many lines long my .csv is.
Any idea?
This did the trick for me !
mat_data = open('file.csv')
data_reader = csv.reader(mat_data)
list_data = list(data_reader)
i=0
value=dict()
for i in range(1,len(list_data)):
value[i]=list_data[i][0]
print value[i]

resolving string (read from database) to list python

I have a list stored in database as varchar. When I read the list value, it is read as string. I'm trying to convert string to a valid list
Here are 2 example lists I have
str_db_bins = [10,20,30,40] (This is string)
The desired output is
str_db_bins_list = [10,20,30,40] (This is list)
I want to convert this to list. When I use split method I get [[,1,0...]] which is not the desired output
2nd situtation is
str_db_lov = ['id','num','val'].
When I use split method, again I get weird output
The desired output is to read this as list
str_db_lov_list = ['id', 'num', 'val']
How do I deal with these situations
Thanks
Pari
str_db_bins_list = eval(str_db_bins)
str_db_lov_list = eval(str_db_lov)

Scala Converting Each Array Element to String and Splitting

I have an array loaded in, and been playing around in the REPL but can't seem to get this to work.
My array looks like this:
record_id|string|FALSE|1|
offer_id|decimal|FALSE|1|1,1
decision_id|decimal|FALSE|1|1,1
offer_type_cd|integer|FALSE|1|1,1
promo_id|decimal|FALSE|1|1,1
pymt_method_type_cd|decimal|FALSE|1|1,1
cs_result_id|decimal|FALSE|1|1,1
cs_result_usage_type_cd|decimal|FALSE|1|1,1
rate_index_type_cd|decimal|FALSE|1|1,1
sub_product_id|decimal|FALSE|1|1,1
campaign_id|decimal|FALSE|1|1,1
When I run my command:
for(i <- 0 until schema.length){
val convert = schema(i).toString;
convert.split('|').drop(2);
println(convert);
}
It won't drop anything. It also is not splitting it on the |
Strings are immutable, and so split and drop don't mutate the string - they return a new one.
You need to capture the result in a new val
val split = convert.split('|').drop(2);
println(split.mkString(" "));
Consider also defining a lambda function for mapping each item in the array, where intermediate results are passed on with the function,
val res = schema.map(s => s.toString.split('|').drop(2))

matlab array assignment

I'm trying to save all the files in a directory as an array of strings, like this:
files = {'hello.gdf'; 'hello2.gdf'...; ... 'etc.gdf'}
Since I have many directories, I want to do this automatically. This is my code:
gdffiles = dir(fullfile('D:', 'subject', '01', '*.gdf'))
for i=1:size(gdffiles)
files(i) = gdffiles(i).name;
end
I want to assign to files the name of the gdf files found, but I get this message:
??? Subscripted assignment dimension mismatch.
Error in ==> getFiles at 3
files(i) = gdffiles(i).name;
What am I doing wrong? Thanks!
The reason for error:
You try to assign files in the i-th place a string (char array) gdffiles(i).name. However, you are using an array-element assignment (round parenthesis ()). Therefore, you get an error: You can only assign a single char using files(i).
Possible solutions:
You should assign to files using curly braces - since files is a cell array:
files{i} = gdffiles(i).name;
You can achieve the same result without the loop by:
files = { gdffiles(:).name };
Check this solution
path = fullfile('D:', 'subject', '01', '*.gdf');
files = dir(path);
files = struct2cell(files);
files = files( 1, 1:end );
Have you tried this :
ListOfAllFiles = ls('*.gif')
Hope it helps

Resources