How can I convert a comma-separated string to an array?
var array = string.split(',');
MDN reference, mostly helpful for the possibly unexpected behavior of the limit
parameter. (Hint: "a,b,c".split(",", 2)
comes out to ["a", "b"]
, not ["a", "b,c"]
.)
Converting a comma separated string into an array
Modify the string so that it's valid JSON, then JSON.parse()
it:
var str = '"alpha", "beta", "gamma", "delta"';
var json = '[' + str + ']';
var array = JSON.parse(json);
console.log(array[0])
Split a comma-delimited string into an array?
Try explode:
$myString = "9,admin@example.com,8";
$myArray = explode(',', $myString);
print_r($myArray);
Output :
Array
(
[0] => 9
[1] => admin@example.com
[2] => 8
)
Convert array of integers to comma-separated string
var result = string.Join(",", arr);
This uses the following overload of string.Join
:
public static string Join<T>(string separator, IEnumerable<T> values);
What's the most simple way to convert comma separated string to int[]?
string s = "1,5,7";
int[] nums = Array.ConvertAll(s.Split(','), int.Parse);
or, a LINQ-y version:
int[] nums = s.Split(',').Select(int.Parse).ToArray();
But the first one should be a teeny bit faster.
How to convert a string of comma separated numbers to integers?
First split the values with comma using the split()
method like this.
arr.split(',')
After that you will get each value separated in an array which will be accessible by giving array index. So arr[0]
will include 108 and arr[1]
will include 109. Now all that's left to do is parse them individually.
parseInt(arr[0])
parseInt(arr[1])
Converting Comma-Separated String to Numpy Array In Python 3
line.decode()
returns a string (a str
), not a list
. So you are getting back a single entity, not 288 values.
Python has a method associated with strings that will take a string and split it into component parts. Calling the .split()
method and giving it the substring to split upon, in this case a ',' should do the trick.
#Read the line of data from the sensor
line = sensor.readline()
#Decode the line to UTF-8 and print it
lineDecoded = line.decode("UTF-8")
values = [int(i) for i in lineDecoded.split(',')] # <<< this should work
# added a list
# comprehension to
# convert values to integers
x = range(1,289) # <<< this is preferred if you need
# a range of numbers from 1 to 288
y = values
#Plot the data
pg.plot(x, y)
NOTE: as @umutto mentions in a comment above, for plotting purposes, there should be no need to convert the values to a numpy array. A list should do just fine.
But, if for some reason you find that you want/need an array:
y = np.array(values)
Related Topics
How to Check Whether a Radio Button Is Selected With JavaScript
Correct Try...Catch Syntax Using Async/Await
Difference Between Variable Declaration Syntaxes in JavaScript (Including Global Variables)
Is "Clear" a Reserved Word in JavaScript
Performance of CSS Transitions VS. Js Animation Packages
Hide Scrollbar and Show on Hover Like Facebook's New Chat Sidebar
Change Text on Hover, Then Return to the Previous Text
Web Worker Settings for Chrome
Es6 Class Variable Alternatives
Set Keyboard Caret Position in HTML Textbox
How to Match a Whole Word in JavaScript
Finding the Max Value of an Attribute in an Array of Objects
Curly Brackets in Arrow Functions
How to Use Webpack to Generate CSS and Js Separately