Convert string of list to python list [duplicate] - python

This question already has answers here:
Converting a string to a tuple in python
(3 answers)
Closed 8 years ago.
I downloaded a csv table from a database using SQL. One of the fields has values like so:
'[0.1234,0.0,0.0]'
I want to convert this string to a python list to get the first value. I only know how to convert strings to ints and floats... is there any way to de-string this object? The table I got from SQL is from a web-based viewer, I'm not getting it from my command line.

You could take the substring from index 1 to index -1 and then split it using the comma as a delimiter. In python
array = variable[1:-1].split(',')
should work.

If you're sure it is always valid list syntax, you could use"
myList = eval('[0.1234,0.0,0.0]')
Or if the value itself has quotes ' in it, you can slice those off
value = "'[0.1234,0.0,0.0]'"
myList = eval(value[1:-1])
Then to get the first value you just
myList[0]

Related

How to split string before a character then have python create a list from it [duplicate]

This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 1 year ago.
I am trying to make python take a string, separate the characters before another character eg: "10001001010Q1002000293Q100292Q". I want to separate the string before each Q and have python create either a list or another string. I cannot figure this out for the life of me.
You can do this using the split function, give "Q" as a parameter to the split function then you can slice the list to only get the numbers before Q.
num = "10001001010Q1002000293Q100292Q"
print(num.split("Q")[:-1])
Split() function: https://www.w3schools.com/python/ref_string_split.asp
Slicing: https://www.w3schools.com/python/python_strings_slicing.asp
The syntax is str.split("separator").
str = str.split("Q")
Then output will be ['10001001010', '1002000293', '100292', ''].
If you don't need the last empty element then you can write as:
str = str.split("Q")[:-1]

From string to fstring [duplicate]

This question already has answers here:
Transform string to f-string
(5 answers)
Closed 2 years ago.
I'm trying to pull a string from JSON, then convert it to an f string to be used dynamically.
Example
Assigned from JSON I get
whose_fault= "{name} started this whole mess"
How to build a lambda to convert it to an f-string and insert the given variable? I just can't quite get my head around it.
I know similar questions have been asked, but no answer seems to quite work for this.
Better question. What's the most pythonic way to insert a variable into a string (which cannot be initially created as an f-string)?
My goal would be a lambda function if possible.
The point being to insert the same variable into whatever string is given where indicated said string.
There is no such thing as f-string type object in python. Its just a feature to allow you execute a code and format a string.
So if you have a variable x= 2020, then you can create another string that contains the variable x in it. Like
y = f"It is now {x+1}". Now y is a string, not a new object type,not a function

String format: getting u'' inside the final string [duplicate]

This question already has answers here:
Removing u in list
(8 answers)
Closed 3 years ago.
I have a list of id's and I am trying the following below:
final = "ids: {}".format(tuple(id_list))
For some reason I am getting the following:
"ids: (u'213231231', u'weqewqqwe')
Could anyone help out on why the u is coming inside my final string. When I am trying the same in another environment, I get the output without the u''. Any specific reason for this?
Actually it is unicode strings in python
for literal value of string you can fist map with str
>>> final = "ids: {}".format(tuple(map(str, id_list)))
>>> final
"ids: ('213231231', 'weqewqqwe')

Inserting individual characters of a string into an array [duplicate]

This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 4 years ago.
I'm new to Python and I'm attempting to place each individual character of a string into an individual element of an array.
string= 'Hello'
array= []
length_of_string= len(string)-1
for i in range (length_of_string):
array.append(string(i))
print(array)
However when I run this code an error occurs as shown below.
array.append(string(i))
TypeError: 'str' object is not callable
The append function works fine when I append to an array using strings or numbers normally but in this instance it does not work.
What do I have to do to get
['H','e','l','l','o']
You mean string[i] if you wan't the ith element of string (not string(i) -- python isn't matlab). However, it's much faster just to do
list(string) # ['H','e','l','l','o']

How can i combine all the contents of a List to one value Python [duplicate]

This question already has answers here:
Converting a list to a string [duplicate]
(8 answers)
Closed 6 years ago.
If I were to get the input of someone and put it into a list. How would I combine this into one big string.
user_input = input()
listed = list(user_input)
I am having trouble with this since the contents are unknown. Is there anyway to make it one big string again(combining all the contents of the list). Is there anything I can import into my code to do this for me
To join a list together, you can use the join method. Simply use it as a method on whatever string you want to have placed between each entry in the list:
>>> ls = ['Hello,','world!']
>>> ' '.join(ls)
'Hello, world!'

Categories

Resources