So I’m learning Python. I am going through the lessons and ran into a problem where I had to condense a great many target.write() into a single write(), while having a “n” between each user input variable(the object of write()). I came up with: If I try to do: I get an error. But if I type: Then it works
Tag: python-2.x
isinstance() and issubclass() return conflicting results
How do you explain isinstance(Hello,object) returns True whilst issubclass(Hello,object) returns False? and Answer It’s because you are using old-style classes so it doesn’t derive from object. Try this instead: Old-style classes are deprecated and you shouldn’t use them any more. In Python 3.x all classes are new-style and writing (object) is no longer required.
How do you find out what the “system default encoding” is?
The documentation for fileobject.encoding mentions that it can be None, and in that case, the “system default encoding” is used. How can I find out what this encoding is? Answer You should use sys.getdefaultencoding()
Pythonic way to split a list into first and rest in Python 2.x?
I think in Python 3 I’ll be able to do: which is exactly what I want, but I’m using 2.6. For now I’m doing: This is fine, but I was just wondering if there’s something more elegant. Answer Basically the same, except that it’s a oneliner. Tuple assigment rocks. This is a bit longer and less obvious, but generalized for
How do I make Python remember settings?
I wrote the beautiful python example code below. Now how do I make it so when I exit then restart the program it remembers the last position of the scale? Edit: I tried the following code But get the following error Answer Write the scale value to a file and read it in on startup. Here’s one way to do
Django TemplateDoesNotExist?
My local machine is running Python 2.5 and Nginx on Ubuntu 8.10, with Django builded from latest development trunk. For every URL I request, it throws: TemplateDoesNotExist at /appname/path appname/template_name.html Django tried loading these templates, in this order: * Using loader django.template.loaders.filesystem.function: * Using loader django.template.loaders.app_directories.function: TEMPLATE_DIRS (‘/usr/lib/python2.5/site-packages/projectname/templates’,) Is it looking for /usr/lib/python2.5/site-packages/projectname/templates/appname/template_name.html in this case? The weird thing is
How can I force division to be floating point? Division keeps rounding down to 0?
I have two integer values a and b, but I need their ratio in floating point. I know that a < b and I want to calculate a / b, so if I use integer division I’ll always get 0 with a remainder of a. How can I force c to be a floating point number in Python 2 in
How to get string objects instead of Unicode from JSON
I’m using Python 2 to parse JSON from ASCII encoded text files. When loading these files with either json or simplejson, all my string values are cast to Unicode objects instead of string objects. The problem is, I have to use the data with some libraries that only accept string objects. I can’t change the libraries nor update them. Is
What is the best way to remove accents (normalize) in a Python unicode string?
I have a Unicode string in Python, and I would like to remove all the accents (diacritics). I found on the web an elegant way to do this (in Java): convert the Unicode string to its long normalized form (with a separate character for letters and diacritics) remove all the characters whose Unicode type is “diacritic”. Do I need to