Convert list items to numbers

There is a csv file with the below three lines. 8.84,17.22,13.22,3.84 3.99,11.73,19.66,1.27 16.14,18.72,7.43,11.09 I am writing a function which reads lines from a file and appends it to an empty list so that I can use that list for computing mean. B

Take the union of two columns, Python + Pandas

I have a df arranged like follows: x y z 0 a jj Nan 1 b ii mm 2 c kk nn 3 d ii NaN 4 e Nan oo 5 f jj mm 6 g Nan nn The desired output is: x y z w 0 a jj Nan a 1 b ii mm a 2 c kk nn c 3 d ii NaN a 4 e Nan oo e 5 f jj mm a 6 g Nan nn c The logic is to

Empty string instead of an unmatched group error

I have this piece of code: for n in (range(1,10)): new = re.sub(r'(regex(group)regex)?regex', r'something'+str(n)+r'\1', old, count=1) It throws the unmatched group error. But if it is unmatched, I want to add empty string there instead of throwing a

django - set of root paths urls.py

When I enter localhost:8000 or localhost:8000/store, both views are same as home/index.html. I want these paths to set different view. urls.py from django.conf.urls import url from django.contrib import admin urlpatterns = [ url(r'', 'home.views.inde

Checking the integrity of the file with Python

I have a directory with many big files. They have all been created with this line of code: pickle.dump(variable, gzip.open(file_name, 'wb'), -1) So they are basically compressed, serialized variables. Now, at some point, a crash/interruption (or seve

How to create a traceback object

I want to create a traceback like the one returned by sys.exc_info()[2]. I don't want a list of lines, I want an actual traceback object: <traceback object at 0x7f6575c37e48> How can I do this? My goal is to have it include the current stack minus o

Print a string as a hexadecimal literal python

I have a lot of pre-existing code that treats byte arrays as strings, i.e. In [70]: x = '\x01\x41\x42\x43' Which python always prints as: In [71]: x Out[71]: '\x01ABC' This makes debugging a pain, since the strings I print don't look like the literal

Transform the HTML table into JSON

I have a huge HTML Table (about 500,000 rows) that I need to transform into a JSON file. The table looks something like this: <table> <tr> <th>Id</th> <th>Timestamp</th> <th>Artist_Name</th> <th>Tweet_

Python extracting the body from the function to the execution

Given a function in Python defined as follows: a = 3 b = 4 c = 5 def add(): d = a+b+c is it possible to get a code object or similar that gives me: a = 3 b = 4 c = 5 d = a+b+c The function object has a code object associated with it; you can exec tha

Python class variable access performance

I wonder if there is any difference in performance when accessing a class variable (a dict) inside a method of the same class using: self.class_variable_dict.add(some_key, some_value) and ClassName.class_variable_dict.add(some_key, some_value) obviou

appropriate way to stop a demon process

I have a Jython script that I run as a daemon. It starts up, logs into a server and then goes into a loop that checks for things to process, processes them, then sleeps for 5 seconds. I have a cron job that checks every 5 minutes to make sure that th

WebSocket onmessage () not called when messages are sent

I'm using autobahn 0.4.10 (https://github.com/oberstet/Autobahn) as a WebSocket server to send messages to a Google Chrome Extension. I am able to open and close connections using WebSocket(), but when I call autobahn.websocket.WebSocketServerProtoco

Learn Python the hard way, Exercise 10.4

#Combine %r with double-quote and single-quote escapes and print them out. #Compare %r with %s. Notice how %r prints it the way you'd write it in your file, #but %s prints it the way you'd like to see it? This is what I wrote for the exercise: 1) myb

Django module already imported and python

Can anyone help me get rid of these warnings in Django please? /usr/lib/python2.6/site-packages/simplejson-2.1.5-py2.6-linux-x86_64.egg/simplejson/_speedups.py:3: UserWarning: Module taggit was already imported from /var/www/html/matilah/taggit/__ini

question about postgresql link variables

I was looking at the question and decided to try using the bind variables. I use sql = 'insert into abc2 (interfield,textfield) values (%s,%s)' a = time.time() for i in range(10000): #just a wrapper around cursor.execute db.executeUpdateCommand(sql,(

How to remove stdout lines in python?

I have a program that grabs some data through ssh using paramiko: ssh = paramiko.SSHClient() ssh.connect(main.Server_IP, username=main.Username, password=main.Password) ssh_stdin_host, ssh_stdout_host, ssh_stderr_host =ssh_session.exec_command(settin

What is the best way to play audio through Qt?

I am building an application in pyQt4 and I want it to be able to play audio files. I was considering doing this through pyMedia as I could not get anywhere with the documentation, although the QAudio classes did initially look promising. It is impor

How to group tkinter?

I am distributing an app that uses the Python/C API. I have all standard python modules in python31.zip which is basically an archive of the Lib folder in the python install directory. Here is the problem - most common modules like sys and io work fi

3D Math Library for Python

i'm looking for a 3d math library in python or with python bindings. it needs to handle rotation, translation, perspective projection, everything basically. what im NOT looking for is a library aimed at drawing on the screen, googling for hours only