Category Archives: Python

Transforming Curl into Python using Urllib with Sentiment140 API

I am trying to use Python to request data from the Sentiment140 API.
The API is using a Bulk Classification Service (JSON). Within terminal it is working fine

leading to the following response:

I thought I could just use urllib to obtain the same response from my python code. I tried:

The code works yet it does not donate me any results.
Am I missing something?

I think you need to use json here.

Try to do:

and on the top

Python regexp multiple expressions with grouping

I’m trying to match the output given by a Modem when asked approximately the network info, it looks like this:

So I’ve two types of expressions to match, the BCCH & non BCCH. the following code is almost working:

But it seems that BOTH expressions are being matched, & not found fields left blank:

May anyone help? Why such behaviour? I’ve tried changing the order of the expressions, with no luck.

Thanks!

That is how capturing groups work. Since you have five of them, there will always be five parts returned.

Based on your data, I think you could simplify your regex by making the bsic part optional. That way each row would return three parts, the middle one being empty for non BCCH-Carriers.

Sum a multidimensional list in python

How can i compute this :

to obtain this:

thanks

You can use collections.defaultdict


Thanks @raymonad for the alternate, cleaner, solution:

Update method in Python dictionary

I was trying to update values in my dictionary, I came across 2 ways to do so:

What is the difference between them?

The difference is that the second method does not work:

dict.update() expects to find a iterable of key-value pairs, keyword arguments, or another dictionary:

Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.

update() accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs: d.update(red=1, blue=2).

map() is a built-in method that produces a sequence by applying the elements of the second (and subsequent) arguments to the first argument, which must be a callable. Unless your key object is a callable & the value object is a sequence, your first method will fail too.

Demo of a working map() application:

Here map() just produces key-value pairs, which satisfies the dict.update() expectations.

Http Download with Python

Simple download

Download with progress

Connect to mysql database using python with MySQLdb cursor