how i can give a variable for this list - python-2.7

i have list like this
http://google.com:username:password
http://google2.com:username2:password2
how i can give a variable for this list i went give 3 variable Address , username , password
when i do print Address i went this program print for me google.com
and when i do print username i went this program print for me username

As mentioned in the comments, the most straightforward solution may be to put your list into a python dictionary structure with your 3 variables as keys, and each key is associated with individual values:
mydict = [
{
'Address': 'http://google.com',
'username': 'username',
'password': 'password'
},
{
'Address': 'http://google2.com:username2:password2',
'username': 'username2',
'password': 'password2'
}
]
Demo:
We have 2 entries from the original list, so you can specify which entry you want by indexing the dictionary starting from 0 as the first entry
# access 1st entry values
print mydict[0]['Address'] # http://google.com
print mydict[0]['username'] # username
# access 2nd entry values
print mydict[1]['password'] # password2

Related

How to add a dictionary into list?

I have a dictionary in the form of a string and I need to add the string_dictionary to the list as a regular dictionary, because I need to enter the list and then the dictionary, like this:
Source[0]["name"]
But, the dictionary in the list is in "" and python not consider it like a dictionary.
dictionary = "{'name': 'liam', 'last name': 'something'}"
Source = [
dictionary,
]
print(Source)
Output:
["{'name': 'liam', 'last name': 'something'}"]
To parse the string to a dictionary, you can use ast.literal_eval:
from ast import literal_eval
dictionary = "{'name': 'liam', 'last name': 'something'}"
Source = [
literal_eval(dictionary),
]
print(Source[0]["name"])
Prints:
liam

Access Python Dictionary elements containing dot in name

I have to read elements of of the following python dictionary:
dict = {'crawler.cmd': 'OS Path for CMD',
's3.failureBucket': 'Failure Bucket Name',
's3.landingBucket': 'Landing Bucket Name',
's3.rawBucket': 'Raw Bucket Name',
'src.name': 'StreamNameUpd'}
As the element name contains dots so, how would I access element with dot in name?
Given your dictionary:
my_dictionary = {'crawler.cmd': 'OS Path for CMD',
's3.failureBucket': 'Failure Bucket Name',
's3.landingBucket': 'Landing Bucket Name',
's3.rawBucket': 'Raw Bucket Name',
'src.name': 'StreamNameUpd'}
You can access any element just by putting its correspondant key in the square brackets like the following:
my_dictionary['crawler.cmd']
Output:
'OS Path for CMD'
Another way to get this:
my_dictionary.get('crawler.cmd')
With get() function you can also define a default value in case the key not in dict:
my_dictionary.get('crawler.cmd', "my_value")
Output:
'OS Path for CMD'

python paho-mqtt publish NULL value

I'm trying to read from a CSV file and publish row values using paho-mqtt but broker log responds that it cannot update the topic with NULL
for r in reader:
publish.single(strMqttChannel, r[2], hostname=strMqttBroker, auth={'username': 'user', 'password': 'password'})
r[2] is in the simplest case i tried 1
it can also be a minus decimal like -1.5
str(r[2]) also doesn't work
If I pass the value without variable it works like charm:
publish.single(strMqttChannel, "1", hostname=strMqttBroker, auth={'username': 'user', 'password': 'password'})
What am I doing wrong?
r[2].strip()
did the trick....

How can I use a string to access a nested attribute from a form?

I have several elements in a form using Flask WTF with this naming convention...
# req_app_qty
# req_app_note
# req_main_qty
# req_main_note
# req_side_qty
# req_side_note
# req_bread_qty
# req_bread_note
# continued...
I can access the form data manually like this...
print "Manually (qty): " , form.req_app_qty.data # works fine
print "Manually (note): " , form.req_app_note.data # works fine
But I am trying to access this form data in a more automated fashion...
categories = [ "app", "main", "side", "bread", "dessert", "bev", "uten", "cups", "misc"]
for x in categories:
field1 = "req_%s_qty.data" % x # create a string to represent the attributes
field2 = "req_%s_note.data" % x # create a string to represent the attributes
qty_rqst = form.field1.data # fails
rqst_note = form.field2.data # fails
# also tried
print "QTY=", getattr(form, field1) # fails
print "Note:", getattr(form, field2) # fails
I tried these methods above and they have failed...
First method the lines fail with an error stating that the form does not have an attribute 'field1' or 'field2'.
As for second method of accessing form data, the following lines fail with a error stating there is no attribute 'req_app_qty.data'
print "QTY=", getattr(form, field1) # fails
How can I create a string to access these form attributes?
qty_rqst = form.field1.data # fails
This doesn't work because you're trying to access the field field1, which doesn't exist.
print "QTY=", getattr(form, field1) # fails
This doesn't work because you're trying to access the field req_X_qty.data, which doesn't exist.
You need to access fields that exist, e.g.
print 'QTY=', getattr(form, 'req_app_qty').data

making separate array from existing json django

I have this array of JSON:
[{'pk': 4L, 'model': u'logic.member', 'fields': {'profile': 3L, 'name': u'1', 'title': u'Mr', 'dob': datetime.date(1983, 1, 1), 'lastname': u'jkjk', 'redressno': u'jdsfsfkj', 'gender': u'm'}}, {'pk': 5L, 'model': u'logic.member', 'fields': {'profile': 3L, 'name': u'2', 'title': u'Mr', 'dob': datetime.date(1983, 1, 1), 'lastname': u'jkjk', 'redressno': u'jdsfsfkj', 'gender': u'm'}}]
I want to make separate array of JSON for only fields property.
What I tried is:
memarr=[]
for index,a in data1:
print index
print a
memarr[index]=a[index].fields
And it is giving an error of:
too many values to unpack
Please correct.
First of all, data1 is a list, so you can't unpack it into 2 variables.
If you want the index, you have to use something like enumerate.
Second, you can't assign to a list via indexing if the key doesn't exist. You have to append or use another valid list insert method.
Third, a[index].fields doesn't really make sense - there is no key in a that would be associated with an integer index and fields is not an attribute.
You're probably looking for something like this:
memarr = []
for index, a in enumerate(data1):
memarr.append(a['fields'])
So many things wrong with that snippet...
Anyway:
memarr = [a['fields'] for a in data]