Why is myList[1] considered a ‘str’ object? mList[1] returns the first item in the list ‘from form’ but I cannot append to item 1 in the list myList. I need to have a list of lists; so ‘from form’ should be a list. I did this: Answer myList[1] is an element of myList and it…
Tag: python
No such file or directory error
This is the error I am getting: And this is my code: The E:/stuff/log.txt file exists. I can navigate in Windows Explorer and open it so why can’t I open it? EDIT: Output of DIR command: I am running the python script from the cmd like this: Answer Firstly, from above, Windows supports / just fine. Seco…
How to convert an area code into a state using PHP or python?
I have a phone number, including area code. Is there a PHP or python-accessible API or library that will return its corresponding state? For example: Answer http://www.50states.com/areacodes/ I suggest putting everything in an array in a seperate file: If you need the area code, just include the file and use …
Do regular expressions from the re module support word boundaries (b)?
While trying to learn a little more about regular expressions, a tutorial suggested that you can use the b to match a word boundary. However, the following snippet in the Python interpreter does not work as expected: It should have been a match object if anything was matched, but it is None. Is the b expressi…
Print all variables in a class? – Python
I’m making a program that can access data stored inside a class. So for example I have this class: So using this I can call out a single variable like: But if I wanted to print all variables, I’m a little lost. If I run: I get: But I want to be able to print the actual data of those.
Cross-platform way to specify Python interpreter when running with execv
I am currently running a Python scripts both on Linux and Windows 7. The file is executed in an execv style with which I mean that the interpreter is defined in the beginning of the file in a command. In Windows system, the interpreter specification is: However in Linux this needs to be I would like to run th…
Run function from the command line
I have this code: How would I run this directly from the command line? Answer With the -c (command) argument (assuming your file is named foo.py): Alternatively, if you don’t care about namespace pollution: And the middle ground:
How to scroll a tkinter canvas to an absolute position?
I’m using Python and tkinter. I have a Canvas widget that will display just one image. Most times the image will be larger than the canvas dimensions, but sometimes it will be smaller. Let’s just focus on the first case (image larger than canvas). I want to scroll the canvas to an absolute positio…
Is it possible to issue a “VACUUM ANALYZE ” from psycopg2 or sqlalchemy for PostgreSQL?
Well, the question pretty much summarises it. My db activity is very update intensive, and I want to programmatically issue a Vacuum Analyze. However I get an error that says that the query cannot be executed within a transaction. Is there some other way to do it? Answer This is a flaw in the Python DB-API: i…
False or None vs. None or False
This behaviour confuses me. Could someone explain to me why is this happening like this? I expected them to both behave the same. Answer The expression x or y evaluates to x if x is true, or y if x is false. Note that “true” and “false” in the above sentence are talking about “tr…