Questions tagged [python-2.x]

For questions about Python programming that are specific to version 2.x of the language. Use the more generic [python] tag for all Python questions, and only add this tag if your question is version-specific.

Python 2 is the version of the Python programming language which was for a long time the most widely deployed in production environments; but it is now in the process of being displaced by Python 3. The two versions of the language are not compatible, though many aspects of the syntax are identical. The latest, and last, released version of Python 2 is Python 2.7.18.

Official support for Python 2 has ended on January 1, 2020.

See also Python2 or Python3.

For information on Python in general, visit the main Python tag wiki.

Tagging recommendation:

Use the tag for all Python related questions. If you believe your question includes issues specific to the incompatible Python 2.x or Python 3.x, in addition to the main tag, use or . If you believe your question may be even more specific, you can include a version specific tag such as .

Please do not mix (or a more specific tag such as and (ditto) unless you are specifically asking a question about an interoperability problem between versions.

Community

Free Python Programming Books

2871 questions
1488
votes
34 answers

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)

I'm having problems dealing with unicode characters from text fetched from different web pages (on different sites). I am using BeautifulSoup. The problem is that the error is not always reproducible; it sometimes works with some pages, and…
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
909
votes
9 answers

What is __future__ in Python used for and how/when to use it, and how it works

__future__ frequently appears in Python modules. I do not understand what __future__ is for and how/when to use it even after reading the Python's __future__ doc. Can anyone explain with examples? A few answers regarding the basic usage of…
leslie
  • 11,858
  • 7
  • 23
  • 22
858
votes
7 answers

What exactly do "u" and "r" string prefixes do, and what are raw string literals?

While asking this question, I realized I didn't know much about raw strings. For somebody claiming to be a Django trainer, this sucks. I know what an encoding is, and I know what u'' alone does since I get what is Unicode. But what does r'' do…
Bite code
  • 578,959
  • 113
  • 301
  • 329
842
votes
28 answers

What is the difference between range and xrange functions in Python 2.X?

Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about for i in range(0, 20): for i in xrange(0, 20):
Teifion
  • 108,121
  • 75
  • 161
  • 195
791
votes
32 answers

IndentationError: unindent does not match any outer indentation level, although the indentation looks correct

When I compile the Python code below, I get IndentationError: unindent does not match any outer indentation level import sys def Factorial(n): # Return factorial result = 1 for i in range (1,n): result = result * i print…
cbrulak
  • 15,436
  • 20
  • 61
  • 101
783
votes
11 answers

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…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
777
votes
14 answers

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…
MiniQuark
  • 46,633
  • 36
  • 147
  • 183
776
votes
9 answers

What is the difference between dict.items() and dict.iteritems() in Python2?

Are there any applicable differences between dict.items() and dict.iteritems()? From the Python docs: dict.items(): Return a copy of the dictionary’s list of (key, value) pairs. dict.iteritems(): Return an iterator over the dictionary’s (key,…
the wolf
  • 34,510
  • 13
  • 53
  • 71
542
votes
12 answers

Convert a Unicode string to a string in Python (containing extra symbols)

How do you convert a Unicode string (containing extra characters like £ $, etc.) into a Python string?
William Troup
  • 12,739
  • 21
  • 70
  • 98
522
votes
33 answers

No module named MySQLdb

I am using Python version 2.5.4 and install MySQL version 5.0 and Django. Django is working fine with Python, but not MySQL. I am using it in Windows Vista.
jbcedge
  • 18,965
  • 28
  • 68
  • 89
373
votes
12 answers

Setting the correct encoding when piping stdout in Python

When piping the output of a Python program, the Python interpreter gets confused about encoding and sets it to None. This means a program like this: # -*- coding: utf-8 -*- print u"åäö" will work fine when run normally, but fail…
Joakim Lundborg
  • 10,920
  • 6
  • 32
  • 39
337
votes
8 answers

How to pick just one item from a generator?

I have a generator function like the following: def myfunct(): ... yield result The usual way to call this function would be: for r in myfunct(): dostuff(r) My question, is there a way to get just one element from the generator whenever I…
Alexandros
  • 4,425
  • 4
  • 23
  • 21
325
votes
7 answers

How to send POST request?

I found this script online: import httplib, urllib params = urllib.urlencode({'number': 12524, 'type': 'issue', 'action': 'show'}) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} conn =…
user1113569
  • 3,441
  • 2
  • 14
  • 10
301
votes
21 answers

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…
Brutus
  • 7,139
  • 7
  • 36
  • 41
279
votes
9 answers

Safest way to convert float to integer in python?

Python's math module contain handy functions like floor & ceil. These functions take a floating point number and return the nearest integer below or above it. However these functions return the answer as a floating point number. For example: import…
Boaz
  • 25,331
  • 21
  • 69
  • 77
1
2 3
99 100