I ran a survey on Mechanical Turk, and the results were returned to me in a string formatted like this:
[{"Q1_option1":{"Option 1":true},"Q1_option2":{"Option 2":false},"Q2_option1":{"Option 1":true},"Q2_option2":{"Option 2":false}}]
I'm not the sharpest programmer out there, and I'm struggling with how to extract the boolean values from the string. I only need the "true" and "false" values in the order they appear.
I would really appreciate any help!
You can use the regular expression module re to extract the values
import re
string = '[{"Q1_option1":{"Option 1":true},"Q1_option2":{"Option 2":false},"Q2_option1":{"Option 1":true},"Q2_option2":{"Option 2":false}}]'
bool_vals = re.findall("true|false", string)
print(bool_vals)
bool_vals is a list that contains the values in the order they appeared in your input string.
Related
I'm new to python, and I'm trying to check if a String is inside a list.
I have these two variables:
new_filename: 'SOLICITUDES2_20201206.DAT' (str type)
and
downloaded_files:
[['SOLICITUDES-20201207.TXT'], ['SOLICITUDES-20201015.TXT'], ['SOLICITUDES2_20201206.DAT']] (list type)
for checking if the string is inside the list, I'm using the following:
if new_filename in downloaded_files:
print(new_filename,'downloaded')
and I never get inside the if.
But if I do the same, but with hard-coded text, it works:
if ['SOLICITUDES2_20201206.DAT'] in downloaded_files_list:
print(new_filename,'downloaded')
What am I doing wrong?
Thanks!
Your downloaded_files is a list of lists. A list can contain anything insider it, numbers, list, dictionaries, strings and etc. If you are trying to find if your string is inside the list, the if statement will only look for identical matches, i.e., strings.
What I suggest you do is get all the strings into a list instead of a list of lists. You can do it using list comprehension:
downloaded_files = [['SOLICITUDES-20201207.TXT'], ['SOLICITUDES-20201015.TXT'], ['SOLICITUDES2_20201206.DAT']]
downloaded_files_list = [file[0] for file in downloaded_files]
Then, your if statement should work:
new_filename = 'SOLICITUDES2_20201206.DAT'
if new_filename in downloaded_files_list:
print(new_filename,'downloaded')
Your code is asking if a string is in a list of lists of a single string each, which is why it doesn't find any.
I want to find a specific string within a string.
For example, let's say I have the string
string = "username:quantopia;password:blabla
How can I then find quantopia?
I am using python 3.
Update: I am sorry I did not mention what I try before..
string.split('username:',1)[1].split(';',1)[0]
But this look very bad and not efficient, I was hoping for something better.
Just use regex as such:
import re
username = re.search("username:(.*);password", "username:quantopia;password:blabla").group(1)
print("username:", username)
This will output quantopia.
In this expression "username:(.*);password" you are saying "give me everything from username: to ;password" So this is why you're getting quantopia. This might as well be ":(.*);" as it will output the same thing in this case.
The simple solution is:
string = "username:quantopia;password:blabla"
username = "username"
if username in string:
# do work.
You might be better to just use split to create a dictionary so you dont need to use multiple regex to extract different parts of data sets. The below will split stirng into key value pairs then split key value pairs then pass the list of lists to dict to create a dictionary.
string = "username:quantopia;password:blabla"
data = dict([pairs.split(':') for pairs in string.split(';')])
print(f'username is "{data["username"]}" and password is "{data["password"]}"')
OUTPUT
username is "quantopia" and password is "blabla"
Lets say I have string: s = '{1} goes to {0}'
And I want to format this string with list: l = ['Hollywood', 'Frankie']
I cannot modify string and list both. Is there way to write simple piece of code to handle this case?
PS. I know about question "Python Format String with List", but it is not what Im asking.
Use the unpack operator * when passing the list to the format method.
s = '{1} goes to {0}'
l = ['Hollywood', 'Frankie']
print(s.format(*l))
This outputs:
Frankie goes to Hollywood
I'm new to Scala and unsure of how to achieve the following
I have the String
val output = "6055039\n3000457596\n3000456748\n180013\n"
I want to extract the numbers separated by \n and store them in an Array
output.split("\n").map(_.toInt)
Or only
output.split("\n")
if you want to keep the numbers in String format. Note that .toInt throws. You might want to wrap it accordingly.
I have a velocity variable, like this:
$cur_record.getFieldValue("SelectRoles", $locale)
that is supposed to be an array. If I print its value, (just by putting $cur_record.getFieldValue("SelectRoles", $locale) in the code) i get:
["Accountant","Cashier"]
now, i want to iterate those 2 values, Accountant and Cashier, but it seems to be a String, not an Array, how can i convert that to an array so I can iterate it?..
I have tried to iterate it, but does not work, like this:
#foreach($bla_role in $cur_record.getFieldValue("SelectRoles", $locale))
$bla_role
#end
Also tried to get the value, as if it were an array, does not work either:
$cur_record.getFieldValue("SelectRoles", $locale).get(0)
I've tried setting it to another variable, like this:
#set($roleval = $cur_record.getFieldValue("SelectRoles", $locale))
$roleval.get(0)
but it does not work, but if i set a string, as the value is printed (the value hard coded), it does work!, like this:
#set($roleval = ["Accountant","Cashier"])
$roleval.get(0)
I dont know if I have to escape something, or I am missing something, can some one help me?
thank you!
You're trying to parse a String array that was previously serialized to String.
The following snippet uses substring, split and replace String methods to parse it.
#set($roleval = '["Accountant","Cashier"]')
#set($rolevalLengthMinusOne = $roleval.length() - 1)
#set($roles = $roleval.substring(1, $rolevalLengthMinusOne).split(","))
#foreach($role in $roles)
<h1>$role.replace('"',"")</h1>
#end
At first I tried to use #evaluate to parse it, but I ended up with these String methods.