title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
list
return eats exception
517,060
<p>I found the following behavior at least <em>weird</em>:</p> <pre><code>def errors(): try: ErrorErrorError finally: return 10 print errors() # prints: 10 # It should raise: NameError: name 'ErrorErrorError' is not defined </code></pre> <p>The exception disappears when you use <code>return</...
13
2009-02-05T17:59:35Z
517,082
<blockquote> <p>The exception disappears when you use <code>return</code> inside a <code>finally</code> clause. .. Is that documented anywhere?</p> </blockquote> <p><a href="http://docs.python.org/reference/compound_stmts.html#the-try-statement" rel="nofollow">It is:</a></p> <blockquote> <p>If finally is present,...
40
2009-02-05T18:03:39Z
[ "python", "exception", "return", "finally" ]
return eats exception
517,060
<p>I found the following behavior at least <em>weird</em>:</p> <pre><code>def errors(): try: ErrorErrorError finally: return 10 print errors() # prints: 10 # It should raise: NameError: name 'ErrorErrorError' is not defined </code></pre> <p>The exception disappears when you use <code>return</...
13
2009-02-05T17:59:35Z
518,359
<p>Here is an interesting comparison for return in finally block, among - Java/C#/Python/JavaScript: <a href="http://www.gettingclever.com/2008/07/return-from-finally.html" rel="nofollow">http://www.gettingclever.com/2008/07/return-from-finally.html</a></p>
5
2009-02-05T22:52:30Z
[ "python", "exception", "return", "finally" ]
return eats exception
517,060
<p>I found the following behavior at least <em>weird</em>:</p> <pre><code>def errors(): try: ErrorErrorError finally: return 10 print errors() # prints: 10 # It should raise: NameError: name 'ErrorErrorError' is not defined </code></pre> <p>The exception disappears when you use <code>return</...
13
2009-02-05T17:59:35Z
525,967
<p>You asked about the Python developers' reasoning. I can't speak for them, but no other behavior makes sense. A function can either return a value, or it can raise an exception; it can't do both. The purpose of a "finally" clause is to provide cleanup code that is "guaranteed" to be run, regardless of exceptions. ...
23
2009-02-08T16:15:35Z
[ "python", "exception", "return", "finally" ]
How do I write output in same place on the console?
517,127
<p>I am new to python and am writing some scripts to automate downloading files from FTP servers, etc. I want to show the progress of the download, but I want it to stay in the same position, such as:</p> <p>output:</p> <blockquote> <p>Downloading File FooFile.txt [47%]</p> </blockquote> <p>I'm trying to avoid som...
82
2009-02-05T18:11:47Z
517,148
<p>Print the backspace character <code>\b</code> several times, and then overwrite the old number with the new number.</p>
10
2009-02-05T18:14:06Z
[ "python", "console-output" ]
How do I write output in same place on the console?
517,127
<p>I am new to python and am writing some scripts to automate downloading files from FTP servers, etc. I want to show the progress of the download, but I want it to stay in the same position, such as:</p> <p>output:</p> <blockquote> <p>Downloading File FooFile.txt [47%]</p> </blockquote> <p>I'm trying to avoid som...
82
2009-02-05T18:11:47Z
517,179
<p>Use a terminal-handling library like the <a href="http://docs.python.org/library/curses.html">curses module</a>:</p> <blockquote> <p>The curses module provides an interface to the curses library, the de-facto standard for portable advanced terminal handling.</p> </blockquote>
19
2009-02-05T18:19:09Z
[ "python", "console-output" ]
How do I write output in same place on the console?
517,127
<p>I am new to python and am writing some scripts to automate downloading files from FTP servers, etc. I want to show the progress of the download, but I want it to stay in the same position, such as:</p> <p>output:</p> <blockquote> <p>Downloading File FooFile.txt [47%]</p> </blockquote> <p>I'm trying to avoid som...
82
2009-02-05T18:11:47Z
517,207
<p>You can also use the carriage return:</p> <pre><code>sys.stdout.write("Download progress: %d%% \r" % (progress) ) sys.stdout.flush() </code></pre>
142
2009-02-05T18:22:47Z
[ "python", "console-output" ]
How do I write output in same place on the console?
517,127
<p>I am new to python and am writing some scripts to automate downloading files from FTP servers, etc. I want to show the progress of the download, but I want it to stay in the same position, such as:</p> <p>output:</p> <blockquote> <p>Downloading File FooFile.txt [47%]</p> </blockquote> <p>I'm trying to avoid som...
82
2009-02-05T18:11:47Z
517,523
<p>I like the following:</p> <pre><code>print 'Downloading File FooFile.txt [%d%%]\r'%i, </code></pre> <p>Demo:</p> <pre><code>import time for i in range(100): time.sleep(0.1) print 'Downloading File FooFile.txt [%d%%]\r'%i, </code></pre>
8
2009-02-05T19:29:23Z
[ "python", "console-output" ]
How do I write output in same place on the console?
517,127
<p>I am new to python and am writing some scripts to automate downloading files from FTP servers, etc. I want to show the progress of the download, but I want it to stay in the same position, such as:</p> <p>output:</p> <blockquote> <p>Downloading File FooFile.txt [47%]</p> </blockquote> <p>I'm trying to avoid som...
82
2009-02-05T18:11:47Z
7,975,759
<pre><code>#kinda like the one above but better :P from __future__ import print_function from time import sleep for i in range(101): str1="Downloading File FooFile.txt [{}%]".format(i) back="\b"*len(str1) print(str1, end="") sleep(0.1) print(back, end="") </code></pre>
7
2011-11-02T04:14:27Z
[ "python", "console-output" ]
HOWTO: Write Python API wrapper?
517,237
<p>I'd like to write a python library to wrap a REST-style API offered by a particular Web service. Does anyone know of any good learning resources for such work, preferably aimed at intermediate Python programmers?</p> <p>I'd like a good article on the subject, but I'd settle for nice, clear code examples.</p> <p><s...
21
2009-02-05T18:29:07Z
517,457
<p>I can't point you to any article on how to do it, but I think there are a few libraries that can be good models on how to design your own.</p> <p><a href="http://pyaws.sourceforge.net/" rel="nofollow">PyAws</a> for example. I didn't see the source code so I can't tell you how good it is as code example, but the fea...
2
2009-02-05T19:14:52Z
[ "python", "web-services", "api", "rest" ]
HOWTO: Write Python API wrapper?
517,237
<p>I'd like to write a python library to wrap a REST-style API offered by a particular Web service. Does anyone know of any good learning resources for such work, preferably aimed at intermediate Python programmers?</p> <p>I'd like a good article on the subject, but I'd settle for nice, clear code examples.</p> <p><s...
21
2009-02-05T18:29:07Z
518,161
<p>My favorite combination is httplib2 (or pycurl for performance) and simplejson. As REST is more "a way of design" then a real "protocol" there is not really a reusable thing (that I know of). On Ruby you have something like ActiveResource. And to be honest, even that would just expose some tables as a webservice, wh...
2
2009-02-05T22:01:01Z
[ "python", "web-services", "api", "rest" ]
HOWTO: Write Python API wrapper?
517,237
<p>I'd like to write a python library to wrap a REST-style API offered by a particular Web service. Does anyone know of any good learning resources for such work, preferably aimed at intermediate Python programmers?</p> <p>I'd like a good article on the subject, but I'd settle for nice, clear code examples.</p> <p><s...
21
2009-02-05T18:29:07Z
523,376
<p><a href="http://learn-rest.blogspot.com/2008/02/using-rest-in-python.html" rel="nofollow">This tutorial page</a> could be a good starting place (but it doesn't contain everything you need).</p>
1
2009-02-07T07:19:31Z
[ "python", "web-services", "api", "rest" ]
HOWTO: Write Python API wrapper?
517,237
<p>I'd like to write a python library to wrap a REST-style API offered by a particular Web service. Does anyone know of any good learning resources for such work, preferably aimed at intermediate Python programmers?</p> <p>I'd like a good article on the subject, but I'd settle for nice, clear code examples.</p> <p><s...
21
2009-02-05T18:29:07Z
981,474
<p>You should take a look at PyFacebook. This is a python wrapper for the Facebook API, and it's one of the most nicely done API's I have ever used.</p>
0
2009-06-11T14:34:57Z
[ "python", "web-services", "api", "rest" ]
HOWTO: Write Python API wrapper?
517,237
<p>I'd like to write a python library to wrap a REST-style API offered by a particular Web service. Does anyone know of any good learning resources for such work, preferably aimed at intermediate Python programmers?</p> <p>I'd like a good article on the subject, but I'd settle for nice, clear code examples.</p> <p><s...
21
2009-02-05T18:29:07Z
3,374,030
<p>You could checkout <a href="http://github.com/ryanmcgrath/pythentic_jobs" rel="nofollow">pythenic jobs</a>, a nice, simple, but well-formed "Python wrapper around the Authentic Jobs ... API" as a good example. That's what I'm doing now :)</p>
0
2010-07-30T17:37:50Z
[ "python", "web-services", "api", "rest" ]
Storing a binary hash value in a Django model field
517,349
<p>I have a twenty byte hex hash that I would like to store in a django model. If I use a text field, it's interpreted as unicode and it comes back garbled. </p> <p>Currently I'm encoding it and decoding it, which really clutters up the code, because I have to be able to filter by it.</p> <pre><code>def get_changese...
6
2009-02-05T18:51:49Z
517,492
<p>I'm assuming if you were writing raw SQL you'd be using a Postgres bytea or a MySQL VARBINARY. There's a <a href="http://code.djangoproject.com/ticket/2417" rel="nofollow">ticket with a patch</a> (marked "needs testing") that purportedly makes a field like this (Ticket 2417: Support for binary type fields (aka: byte...
4
2009-02-05T19:23:07Z
[ "python", "django", "encoding", "django-models", "binary-data" ]
Storing a binary hash value in a Django model field
517,349
<p>I have a twenty byte hex hash that I would like to store in a django model. If I use a text field, it's interpreted as unicode and it comes back garbled. </p> <p>Currently I'm encoding it and decoding it, which really clutters up the code, because I have to be able to filter by it.</p> <pre><code>def get_changese...
6
2009-02-05T18:51:49Z
517,632
<p>You could also write your own custom <a href="http://docs.djangoproject.com/en/dev/topics/db/managers/#topics-db-managers" rel="nofollow">Model Manager</a> that does the escaping and unescaping for you.</p>
3
2009-02-05T19:52:58Z
[ "python", "django", "encoding", "django-models", "binary-data" ]
Storing a binary hash value in a Django model field
517,349
<p>I have a twenty byte hex hash that I would like to store in a django model. If I use a text field, it's interpreted as unicode and it comes back garbled. </p> <p>Currently I'm encoding it and decoding it, which really clutters up the code, because I have to be able to filter by it.</p> <pre><code>def get_changese...
6
2009-02-05T18:51:49Z
518,704
<p>"I have a twenty byte hex hash that I would like to store in a django model."</p> <p>Django does this. They use hex digests, which are -- technically -- strings. Not bytes.</p> <p>Do not use <code>someHash.digest()</code> -- you get bytes, which you cannot easily store.</p> <p>Use <code>someHash.hexdigest()</co...
3
2009-02-06T00:59:44Z
[ "python", "django", "encoding", "django-models", "binary-data" ]
Storing a binary hash value in a Django model field
517,349
<p>I have a twenty byte hex hash that I would like to store in a django model. If I use a text field, it's interpreted as unicode and it comes back garbled. </p> <p>Currently I'm encoding it and decoding it, which really clutters up the code, because I have to be able to filter by it.</p> <pre><code>def get_changese...
6
2009-02-05T18:51:49Z
12,496,786
<p>If this issue is still of interest, Disqus' <code>django-bitfield</code> fits the bill:</p> <p><a href="https://github.com/disqus/django-bitfield" rel="nofollow">https://github.com/disqus/django-bitfield</a></p> <p>... the example code on GitHub is a little confusing at first w/r/t the modules' actual function, be...
1
2012-09-19T14:22:44Z
[ "python", "django", "encoding", "django-models", "binary-data" ]
String formatting in Python
517,355
<p>I want to do something like String.Format ("[{0}, {1}, {2}]", 1, 2, 3) which returns:</p> <pre><code>[1, 2, 3] </code></pre> <p>How do I do this in Python?</p>
21
2009-02-05T18:53:29Z
517,363
<p>You haven't formulated yourself very commendably, but I'll venture a guess this is what you're looking for:</p> <pre><code>foo = "Hello" bar = "world" baz = 2 print "%s, %s number %d" % (foo, bar, baz) </code></pre>
1
2009-02-05T18:56:15Z
[ "python", "string", "formatting" ]
String formatting in Python
517,355
<p>I want to do something like String.Format ("[{0}, {1}, {2}]", 1, 2, 3) which returns:</p> <pre><code>[1, 2, 3] </code></pre> <p>How do I do this in Python?</p>
21
2009-02-05T18:53:29Z
517,372
<p>You're looking for string formatting, which in python is based on the sprintf function in C.</p> <pre><code>print "[%s, %s, %s]" % (1, 2, 3) </code></pre> <p>For a complete reference look here: <a href="http://docs.python.org/library/stdtypes.html#string-formatting" rel="nofollow">http://docs.python.org/librar...
16
2009-02-05T18:59:07Z
[ "python", "string", "formatting" ]
String formatting in Python
517,355
<p>I want to do something like String.Format ("[{0}, {1}, {2}]", 1, 2, 3) which returns:</p> <pre><code>[1, 2, 3] </code></pre> <p>How do I do this in Python?</p>
21
2009-02-05T18:53:29Z
517,471
<p>The previous answers have used % formatting, which is being phased out in Python 3.0+. Assuming you're using Python 2.6+, a more future-proof formatting system is described here:</p> <p><a href="http://docs.python.org/library/string.html#formatstrings">http://docs.python.org/library/string.html#formatstrings</a></...
53
2009-02-05T19:16:29Z
[ "python", "string", "formatting" ]
String formatting in Python
517,355
<p>I want to do something like String.Format ("[{0}, {1}, {2}]", 1, 2, 3) which returns:</p> <pre><code>[1, 2, 3] </code></pre> <p>How do I do this in Python?</p>
21
2009-02-05T18:53:29Z
628,974
<p>You can do it three ways:</p> <p><hr /></p> <p>Use Python's automatic pretty printing:</p> <pre><code>print [1, 2, 3] # Prints [1, 2, 3] </code></pre> <p>Showing the same thing with a variable:</p> <pre><code>numberList = [1, 2] numberList.append(3) print numberList # Prints [1, 2, 3] </code></pre> <p><hr ...
25
2009-03-10T04:54:35Z
[ "python", "string", "formatting" ]
String formatting in Python
517,355
<p>I want to do something like String.Format ("[{0}, {1}, {2}]", 1, 2, 3) which returns:</p> <pre><code>[1, 2, 3] </code></pre> <p>How do I do this in Python?</p>
21
2009-02-05T18:53:29Z
11,404,948
<p>To print elements sequentially use {} without specifying the index</p> <pre><code>print('[{},{},{}]'.format(1,2,3)) </code></pre> <p>(works since python 2.7 and python 3.1)</p>
3
2012-07-10T00:05:15Z
[ "python", "string", "formatting" ]
String formatting in Python
517,355
<p>I want to do something like String.Format ("[{0}, {1}, {2}]", 1, 2, 3) which returns:</p> <pre><code>[1, 2, 3] </code></pre> <p>How do I do this in Python?</p>
21
2009-02-05T18:53:29Z
19,296,530
<p>If you don't know how many items are in list, this aproach is the most universal</p> <pre><code>&gt;&gt;&gt; '[{0}]'.format(', '.join([str(i) for i in [1,2,3]])) '[1, 2, 3]' </code></pre> <p>It is mouch simplier for list of strings</p> <pre><code>&gt;&gt;&gt; '[{0}]'.format(', '.join(['a','b','c'])) '[a, b, c]' ...
0
2013-10-10T13:03:32Z
[ "python", "string", "formatting" ]
String formatting in Python
517,355
<p>I want to do something like String.Format ("[{0}, {1}, {2}]", 1, 2, 3) which returns:</p> <pre><code>[1, 2, 3] </code></pre> <p>How do I do this in Python?</p>
21
2009-02-05T18:53:29Z
28,747,429
<p>I think that this combination is missing :P</p> <pre><code>"[{0}, {1}, {2}]".format(*[1, 2, 3]) </code></pre>
1
2015-02-26T16:18:20Z
[ "python", "string", "formatting" ]
Execute a string as a command in python
517,491
<p>I am developing my stuff in python. In this process I encountered a situation where I have a string called "import django". And I want to validate this string. Which means, I want to check whether the module mentioned('django' in this case) is in the python-path. How can I do it?</p>
1
2009-02-05T19:23:05Z
517,530
<p>I doub't that it's safe, but it's the most naïve solution:</p> <pre><code>try: exec('import django') except ImportError: print('no django') </code></pre>
1
2009-02-05T19:30:45Z
[ "python", "django", "path" ]
Execute a string as a command in python
517,491
<p>I am developing my stuff in python. In this process I encountered a situation where I have a string called "import django". And I want to validate this string. Which means, I want to check whether the module mentioned('django' in this case) is in the python-path. How can I do it?</p>
1
2009-02-05T19:23:05Z
517,592
<p>My previous answer was wrong -- i didn't think to test my code. This actually works, though: look at the <a href="http://docs.python.org/library/imp.html" rel="nofollow">imp</a> module.</p> <p>To just check for the module's importability in the current sys.path: </p> <pre><code>try: imp.find_module('django', ...
14
2009-02-05T19:44:22Z
[ "python", "django", "path" ]
Execute a string as a command in python
517,491
<p>I am developing my stuff in python. In this process I encountered a situation where I have a string called "import django". And I want to validate this string. Which means, I want to check whether the module mentioned('django' in this case) is in the python-path. How can I do it?</p>
1
2009-02-05T19:23:05Z
522,135
<p>If a module's name is available as string you can import it using the built-in <code>__import__</code> function.</p> <pre><code>module = __import__("module name", {}, {}, [], -1) </code></pre> <p>For example,</p> <pre><code>os = __import__("os", {}, {}, [], -1) </code></pre>
1
2009-02-06T20:55:26Z
[ "python", "django", "path" ]
What is the best way to remove accents in a Python unicode string?
517,923
<p>I have a Unicode string in Python, and I would like to remove all the accents (diacritics).</p> <p>I found on the Web an elegant way to do this in Java:</p> <ol> <li>convert the Unicode string to its long normalized form (with a separate character for letters and diacritics)</li> <li>remove all the characters whos...
230
2009-02-05T21:10:40Z
517,974
<p>I just found this answer on the Web:</p> <pre><code>import unicodedata def remove_accents(input_str): nfkd_form = unicodedata.normalize('NFKD', input_str) only_ascii = nfkd_form.encode('ASCII', 'ignore') return only_ascii </code></pre> <p>It works fine (for French, for example), but I think the second...
83
2009-02-05T21:19:34Z
[ "python", "python-3.x", "unicode", "python-2.x", "diacritics" ]
What is the best way to remove accents in a Python unicode string?
517,923
<p>I have a Unicode string in Python, and I would like to remove all the accents (diacritics).</p> <p>I found on the Web an elegant way to do this in Java:</p> <ol> <li>convert the Unicode string to its long normalized form (with a separate character for letters and diacritics)</li> <li>remove all the characters whos...
230
2009-02-05T21:10:40Z
518,232
<p>How about this:</p> <pre><code>import unicodedata def strip_accents(s): return ''.join(c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn') </code></pre> <p>This works on greek letters, too:</p> <pre><code>&gt;&gt;&gt; strip_accents(u"A \u00c0 \u0394 \u038E") u'A A \...
164
2009-02-05T22:17:22Z
[ "python", "python-3.x", "unicode", "python-2.x", "diacritics" ]
What is the best way to remove accents in a Python unicode string?
517,923
<p>I have a Unicode string in Python, and I would like to remove all the accents (diacritics).</p> <p>I found on the Web an elegant way to do this in Java:</p> <ol> <li>convert the Unicode string to its long normalized form (with a separate character for letters and diacritics)</li> <li>remove all the characters whos...
230
2009-02-05T21:10:40Z
2,633,310
<p><a href="https://pypi.python.org/pypi/Unidecode">Unidecode</a> is the correct answer for this. It transliterates any unicode string into the closest possible representation in ascii text.</p>
155
2010-04-13T21:21:14Z
[ "python", "python-3.x", "unicode", "python-2.x", "diacritics" ]
What is the best way to remove accents in a Python unicode string?
517,923
<p>I have a Unicode string in Python, and I would like to remove all the accents (diacritics).</p> <p>I found on the Web an elegant way to do this in Java:</p> <ol> <li>convert the Unicode string to its long normalized form (with a separate character for letters and diacritics)</li> <li>remove all the characters whos...
230
2009-02-05T21:10:40Z
15,547,803
<p>This handles not only accents, but also "strokes" (as in ø etc.):</p> <pre><code>import unicodedata as ud def rmdiacritics(char): ''' Return the base character of char, by "removing" any diacritics like accents or curls and strokes and the like. ''' desc = ud.name(unicode(char)) cutoff = d...
9
2013-03-21T12:39:18Z
[ "python", "python-3.x", "unicode", "python-2.x", "diacritics" ]
What is the best way to remove accents in a Python unicode string?
517,923
<p>I have a Unicode string in Python, and I would like to remove all the accents (diacritics).</p> <p>I found on the Web an elegant way to do this in Java:</p> <ol> <li>convert the Unicode string to its long normalized form (with a separate character for letters and diacritics)</li> <li>remove all the characters whos...
230
2009-02-05T21:10:40Z
17,069,876
<p>In response to @MiniQuark's answer:</p> <p>I was trying to read in a csv file that was half-French (containing accents) and also some strings which would eventually become integers and floats. As a test, I created a <code>test.txt</code> file that looked like this:</p> <blockquote> <p>Montréal, über, 12.89, MÃ...
8
2013-06-12T15:48:48Z
[ "python", "python-3.x", "unicode", "python-2.x", "diacritics" ]
What is the best way to remove accents in a Python unicode string?
517,923
<p>I have a Unicode string in Python, and I would like to remove all the accents (diacritics).</p> <p>I found on the Web an elegant way to do this in Java:</p> <ol> <li>convert the Unicode string to its long normalized form (with a separate character for letters and diacritics)</li> <li>remove all the characters whos...
230
2009-02-05T21:10:40Z
31,607,735
<p>Actually I work on project compatible python 2.6, 2.7 and 3.4 and I have to create IDs from free user entries. </p> <p>Thanks to you, I have created this function that works wonders.</p> <pre><code>import re import unicodedata def strip_accents(text): """ Strip accents from input String. :param text:...
6
2015-07-24T10:08:14Z
[ "python", "python-3.x", "unicode", "python-2.x", "diacritics" ]
What is the best way to remove accents in a Python unicode string?
517,923
<p>I have a Unicode string in Python, and I would like to remove all the accents (diacritics).</p> <p>I found on the Web an elegant way to do this in Java:</p> <ol> <li>convert the Unicode string to its long normalized form (with a separate character for letters and diacritics)</li> <li>remove all the characters whos...
230
2009-02-05T21:10:40Z
31,609,516
<p>Some languages have combining diacritics as language letters and accent diacritics to specify accent.</p> <p>I think it is more safe to specify explicitly what diactrics you want to strip:</p> <pre><code>def strip_accents(string, accents=('COMBINING ACUTE ACCENT', 'COMBINING GRAVE ACCENT', 'COMBINING TILDE')): ...
0
2015-07-24T11:34:02Z
[ "python", "python-3.x", "unicode", "python-2.x", "diacritics" ]
What is the best way to remove accents in a Python unicode string?
517,923
<p>I have a Unicode string in Python, and I would like to remove all the accents (diacritics).</p> <p>I found on the Web an elegant way to do this in Java:</p> <ol> <li>convert the Unicode string to its long normalized form (with a separate character for letters and diacritics)</li> <li>remove all the characters whos...
230
2009-02-05T21:10:40Z
39,059,237
<p>I'm surprised no one suggested a simple: </p> <pre><code>from unidecode import unidecode s="Montréal, über, 12.89, Mère, Françoise, noël, 889" #s.encode("ascii") #doesn't work - traceback t=unidecode(s) t.encode("ascii") #works fine, because all non-ASCII from s are replaced with their equivalents print(t) ...
3
2016-08-20T22:28:03Z
[ "python", "python-3.x", "unicode", "python-2.x", "diacritics" ]
What is the best way to remove accents in a Python unicode string?
517,923
<p>I have a Unicode string in Python, and I would like to remove all the accents (diacritics).</p> <p>I found on the Web an elegant way to do this in Java:</p> <ol> <li>convert the Unicode string to its long normalized form (with a separate character for letters and diacritics)</li> <li>remove all the characters whos...
230
2009-02-05T21:10:40Z
39,612,904
<p>import unicodedata</p> <p>s = 'Émission'</p> <p>search_string = ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn'))</p> <p>For PYTHON 3.X print (search_string) For PYTHON 2.X print search_string</p>
0
2016-09-21T09:41:47Z
[ "python", "python-3.x", "unicode", "python-2.x", "diacritics" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
517,992
<p>Well, here's a quick hack:</p> <pre><code>&gt;&gt;&gt; clear = "\n" * 100 &gt;&gt;&gt; print clear &gt;&gt;&gt; ...do some other stuff... &gt;&gt;&gt; print clear </code></pre> <p>Or to save some typing, put this file in your python search path:</p> <pre><code># wiper.py class Wipe(object): def __repr__(self)...
56
2009-02-05T21:22:43Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
518,007
<p>As you mentioned, you can do a system call:</p> <pre><code>&gt;&gt;&gt; import os &gt;&gt;&gt; clear = lambda: os.system('cls') &gt;&gt;&gt; clear() </code></pre> <p>I am not sure of any other way in Windows.</p>
218
2009-02-05T21:25:08Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
518,401
<p>Use idle. It has many handy features. <kbd>Ctrl+F6</kbd>, for example, resets the console. Closing and opening the console are good ways to clear it.</p>
5
2009-02-05T23:03:55Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
518,540
<p>EDIT: I've just read "windows", this is for linux users, sorry.</p> <p><hr /></p> <p>In bash:</p> <pre><code>#!/bin/bash while [ "0" == "0" ]; do clear $@ while [ "$input" == "" ]; do read -p "Do you want to quit? (y/n): " -n 1 -e input if [ "$input" == "y" ]; then exit 1 ...
1
2009-02-05T23:51:51Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
684,344
<p>here something handy that is a little more cross-platform</p> <pre><code>import os def cls(): os.system('cls' if os.name=='nt' else 'clear') # now, to clear the screen cls() </code></pre>
111
2009-03-26T02:42:42Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
759,084
<p>Wiper is cool, good thing about it is I don't have to type '()' around it. Here is slight variation to it</p> <pre><code># wiper.py import os class Cls(object): def __repr__(self):         os.system('cls') return '' </code></pre> <p>The usage is quite simple:</p> <pre><code>&gt;&gt;&gt; cls = Cls(...
6
2009-04-17T04:58:19Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
3,917,856
<p>Although this is an older question, I thought I'd contribute something summing up what I think were the best of the other answers and add a wrinkle of my own by suggesting that you put these command(s) into a file and set your PYTHONSTARTUP environment variable to point to it. Since I'm on Windows at the moment, it'...
22
2010-10-12T18:36:11Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
5,369,197
<p>This should be cross platform, and also uses the preferred <code>subprocess.call</code> instead of <code>os.system</code> as per <a href="http://docs.python.org/library/os.html#os.system" rel="nofollow">the <code>os.system</code> docs</a>. Should work in Python >= 2.4.</p> <pre><code>import subprocess import os if...
1
2011-03-20T14:51:23Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
7,265,491
<p><strong>Here are two nice ways of doing that:</strong></p> <p><strong>1.</strong> </p> <pre><code>import os # Clear Windows command prompt. if (os.name in ('ce', 'nt', 'dos')): os.system('cls') # Clear the Linux terminal. elif ('posix' in os.name): os.system('clear') </code></pre> <p><strong>2.</strong>...
1
2011-09-01T02:13:46Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
7,987,728
<p>How about this for a clear</p> <pre><code>- os.system('cls') </code></pre> <p>That is about as short as could be!</p>
1
2011-11-02T21:48:09Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
8,055,652
<p>I'm using MINGW/BASH on Windows XP, SP3.</p> <p>(stick this in .pythonstartup)<br> # My ctrl-l already kind of worked, but this might help someone else<br> # leaves prompt at bottom of the window though...<br> import readline<br> readline.parse_and_bind('\C-l: clear-screen') </p> <p># This works in BASH because...
3
2011-11-08T18:56:43Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
8,463,527
<p>The OS command <code>clear</code> in Linux and <code>cls</code> in Windows outputs a "magic string" which you can just print. To get the string, execute the command with popen and save it in a variable for later use:</p> <pre><code>from os import popen with popen('clear') as f: clear = f.read() print clear </...
1
2011-12-11T11:19:25Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
9,751,966
<pre><code>&gt;&gt;&gt; ' '*80*25 </code></pre> <p><strong>UPDATE</strong>: 80x25 is unlikely to be the size of console windows, so to get the real console dimensions, use functions from <a href="https://pypi.python.org/pypi/pager" rel="nofollow">pager</a> module. Python doesn't provide anything similar from core dist...
-1
2012-03-17T17:08:50Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
11,526,998
<p>Here's <a href="https://gist.github.com/3130325" rel="nofollow">the definitive solution</a> that merges <strong>all other answers</strong>. Features:</p> <ol> <li>You can <strong>copy-paste</strong> the code into your shell or script.</li> <li><p>You can <strong>use</strong> it as you like:</p> <pre><code>&gt;&gt;...
5
2012-07-17T16:38:19Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
15,623,297
<p>I'm new to python (really really new) and in one of the books I'm reading to get acquainted with the language they teach how to create this little function to clear the console of the visible backlog and past commands and prints:</p> <p>Open shell / Create new document / Create function as follows:</p> <pre><code>...
1
2013-03-25T19:41:24Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
17,536,224
<p>OK, so this is a much less technical answer, but I'm using the Python plugin for Notepad++ and it turns out you can just clear the console manually by right-clicking on it and clicking "clear". Hope this helps someone out there!</p>
0
2013-07-08T21:24:28Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
17,711,382
<p>In Spyder, when you want to clear all the variables in you Variable explorer, simply type <em>global().clear()</em> in the Console, and they will all be gone.</p>
-2
2013-07-17T22:28:54Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
18,846,817
<p>I found the simplest way is just to close the window and run a module/script to reopen the shell.</p>
2
2013-09-17T10:00:57Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
21,210,375
<p>Magic strings are mentioned above - I believe they come from the terminfo database:</p> <p><a href="http://www.google.com/?q=x#q=terminfo" rel="nofollow">http://www.google.com/?q=x#q=terminfo</a></p> <p><a href="http://www.google.com/?q=x#q=tput+command+in+unix" rel="nofollow">http://www.google.com/?q=x#q=tput+com...
1
2014-01-18T21:54:03Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
23,730,811
<p>just use this..</p> <p><code>print '\n'*1000</code></p>
2
2014-05-19T06:15:17Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
29,520,444
<p>I am using Spyder (Python 2.7) and to clean the interpreter console I use either </p> <p>%clear </p> <p>that forces the command line to go to the top and I will not see the previous old commands.</p> <p>or I click "option" on the Console environment and select "Restart kernel" that removes everything.</p>
0
2015-04-08T16:35:45Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
30,619,161
<p>my way of doing this is to write a function like so:</p> <pre><code>import os,subprocess def clear(): if os.name = ('nt','dos'): subprocess.call("cls") elif os.name = ('linux','osx','posix'): subprocess.call("clear") else: print "\n"*120 </code></pre> <p>then call <code>clear()<...
5
2015-06-03T11:44:59Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
31,104,446
<p>I'm not sure if Windows' "shell" supports this, but on Linux:</p> <p><code>print "\033[2J"</code></p> <p><a href="https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes" rel="nofollow">https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes</a></p> <p>In my opinion calling <code>cls</code> with <code>os</code>...
1
2015-06-28T20:31:32Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
31,640,548
<p>If its on mac a simple cmd + k does the trick</p>
0
2015-07-26T18:37:17Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
32,280,047
<p>Quickest and easiest way without a doubt is <kbd>Ctrl</kbd>+<kbd>L</kbd>.</p> <p>This is the same for OS X on the terminal. </p>
5
2015-08-28T21:32:00Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
37,106,601
<p>I use <a href="https://www.iterm2.com/" rel="nofollow">iTerm</a> and the native terminal app for Mac OS.</p> <p>I just press ⌘ + k</p>
3
2016-05-09T01:19:38Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
37,925,455
<p>You have number of ways doing it on Windows:</p> <h1>1. Using Keyboard shortcut:</h1> <pre><code>Press CTRL + L </code></pre> <h1>2. Using system invoke method:</h1> <pre><code>import os cls = lambda: os.system('cls') cls() </code></pre> <h1>3. Using new line print 100 times:</h1> <pre><code>cls = lambda: prin...
4
2016-06-20T14:46:29Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
38,758,248
<p>At the prompt type in "cls" and hit enter. </p>
-1
2016-08-04T03:57:18Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
38,986,254
<p>I might be late to the part but here is a very easy way to do it</p> <p>Type:</p> <pre><code>def cls(): os.system("cls") </code></pre> <p>So what ever you want to clear the screen just type in your code</p> <pre><code>cls() </code></pre> <p>Best way possible! (Credit : <a href="https://www.youtube.com/watch...
-1
2016-08-17T00:05:00Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
39,801,391
<p><a href="https://github.com/adoxa/ansicon/releases" rel="nofollow">https://github.com/adoxa/ansicon/releases</a></p> <p>There is a tool you can install for Windows and then use the following print: print "\x1b[2J"</p> <p>See link for tool below:</p>
-1
2016-09-30T23:22:47Z
[ "windows", "console", "clear", "python" ]
How to clear python interpreter console?
517,970
<p>Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.</p> <p>Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comm...
169
2009-02-05T21:19:20Z
40,116,016
<p>Stumbled upon this: Just keep pressing <kbd>page down</kbd> till the screen is cleared!</p> <p>Then press <kbd>enter</kbd> for the <code>&gt;&gt;&gt;</code> prompt.</p> <p>Surprised nobody has mentioned this yet.</p>
0
2016-10-18T19:00:00Z
[ "windows", "console", "clear", "python" ]
Getting the length of an array in Python
518,021
<p>In <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>, is the following the only way to get the number of elements?</p> <pre><code>arr.__len__() </code></pre> <p>If so, why the strange syntax?</p>
400
2009-02-05T21:26:47Z
518,038
<pre><code>my_list = [1,2,3,4,5] len(my_list) </code></pre> <p>The same works for tuples:</p> <pre><code>my_tuple = (1,2,3,4,5) len(my_tuple) </code></pre> <p>And strings, which are really just arrays of characters:</p> <pre><code>my_string = 'hello world' len(my_string) </code></pre> <p>It was <a href="http://eff...
718
2009-02-05T21:29:48Z
[ "python", "arrays" ]
Getting the length of an array in Python
518,021
<p>In <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>, is the following the only way to get the number of elements?</p> <pre><code>arr.__len__() </code></pre> <p>If so, why the strange syntax?</p>
400
2009-02-05T21:26:47Z
518,039
<p>Just use len(arr):</p> <pre><code>&gt;&gt;&gt; import array &gt;&gt;&gt; arr = array.array('i') &gt;&gt;&gt; arr.append('2') &gt;&gt;&gt; arr.__len__() 1 &gt;&gt;&gt; len(arr) 1 </code></pre>
13
2009-02-05T21:30:05Z
[ "python", "arrays" ]
Getting the length of an array in Python
518,021
<p>In <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>, is the following the only way to get the number of elements?</p> <pre><code>arr.__len__() </code></pre> <p>If so, why the strange syntax?</p>
400
2009-02-05T21:26:47Z
518,053
<p>The preferred way to get the length of any python object is to pass it as an argument to the <code>len</code> function. Internally, python will then try to call the special <code>__len__</code> method of the object that was passed.</p>
19
2009-02-05T21:32:43Z
[ "python", "arrays" ]
Getting the length of an array in Python
518,021
<p>In <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>, is the following the only way to get the number of elements?</p> <pre><code>arr.__len__() </code></pre> <p>If so, why the strange syntax?</p>
400
2009-02-05T21:26:47Z
518,061
<p>The way you take a length of anything for which that makes sense (a list, dictionary, tuple, string, ...) is to call <code>len</code> on it.</p> <pre><code>l = [1,2,3,4] s = 'abcde' len(l) #returns 4 len(s) #returns 5 </code></pre> <p>The reason for the "strange" syntax is that internally python translates <code>l...
32
2009-02-05T21:34:00Z
[ "python", "arrays" ]
Getting the length of an array in Python
518,021
<p>In <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>, is the following the only way to get the number of elements?</p> <pre><code>arr.__len__() </code></pre> <p>If so, why the strange syntax?</p>
400
2009-02-05T21:26:47Z
519,644
<p>Python uses <a href="http://en.wikipedia.org/wiki/Duck_typing">duck typing</a>: it doesn't care about what an object <em>is</em>, as long as it has the appropriate interface for the situation at hand. When you call the built-in function len() on an object, you are actually calling its internal __len__ method. A cust...
18
2009-02-06T09:15:37Z
[ "python", "arrays" ]
Getting the length of an array in Python
518,021
<p>In <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>, is the following the only way to get the number of elements?</p> <pre><code>arr.__len__() </code></pre> <p>If so, why the strange syntax?</p>
400
2009-02-05T21:26:47Z
33,226,444
<p>you can use <code>len(arr)</code> as suggested in previous answers to get the length of the array. In case you want the dimensions of a 2D array you could use <code>arr.shape</code> returns height and width</p>
3
2015-10-20T01:10:57Z
[ "python", "arrays" ]
Getting the length of an array in Python
518,021
<p>In <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>, is the following the only way to get the number of elements?</p> <pre><code>arr.__len__() </code></pre> <p>If so, why the strange syntax?</p>
400
2009-02-05T21:26:47Z
36,571,771
<p><code>len(list_name)</code> function takes list as a parameter and it calls list's <code>__len__()</code> function.</p>
2
2016-04-12T11:20:13Z
[ "python", "arrays" ]
How to work with unsaved many-to-many relations in django?
518,162
<p>I have a couple of models in django which are connected many-to-many. I want to create instances of these models <em>in memory</em>, present them to the user (via custom method-calls inside the view-templates) and if the user is satisfied, save them to the database.</p> <p>However, if I try to do anything on the mo...
4
2009-02-05T22:01:17Z
518,290
<p>I would add a field which indicates whether the objects are "draft" or "live". That way they are persisted across requests, sessions, etc. and django stops complaining.</p> <p>You can then filter your objects to only show "live" objects in public views and only show "draft" objects to the user that created them. T...
6
2009-02-05T22:34:11Z
[ "python", "django", "django-models", "many-to-many" ]
How to work with unsaved many-to-many relations in django?
518,162
<p>I have a couple of models in django which are connected many-to-many. I want to create instances of these models <em>in memory</em>, present them to the user (via custom method-calls inside the view-templates) and if the user is satisfied, save them to the database.</p> <p>However, if I try to do anything on the mo...
4
2009-02-05T22:01:17Z
519,483
<p>I think that using django forms may be the answer, as outlined in <a href="http://docs.djangoproject.com/en/dev/topics/forms/modelforms/" rel="nofollow">this</a> documentation (search for m2m...).</p> <p>Edited to add some explanation for other people who might have the same problem:</p> <p>say you have a model li...
4
2009-02-06T07:52:11Z
[ "python", "django", "django-models", "many-to-many" ]
How can I manipulate lists?
518,679
<p>In the computer algebra system <a href="http://www.sagemath.org/" rel="nofollow">Sage</a>, I need to multiply a list by 2.</p> <p>I tried the code</p> <pre><code>sage: list = [1, 2, 3]; sage: 2 * list </code></pre> <p>which returns</p> <pre><code>[1, 2, 3, 1, 2, 3] </code></pre> <p>How can I just multiply eac...
1
2009-02-06T00:48:23Z
518,895
<p>Do you want to multiply each element by 2? That would be:</p> <pre><code>[2*i for i in List] </code></pre>
3
2009-02-06T02:35:43Z
[ "python", "list" ]
How can I manipulate lists?
518,679
<p>In the computer algebra system <a href="http://www.sagemath.org/" rel="nofollow">Sage</a>, I need to multiply a list by 2.</p> <p>I tried the code</p> <pre><code>sage: list = [1, 2, 3]; sage: 2 * list </code></pre> <p>which returns</p> <pre><code>[1, 2, 3, 1, 2, 3] </code></pre> <p>How can I just multiply eac...
1
2009-02-06T00:48:23Z
1,262,098
<p>Or:</p> <pre><code>import numpy numpy.multiply(List, 2) </code></pre>
1
2009-08-11T18:19:49Z
[ "python", "list" ]
How can I manipulate lists?
518,679
<p>In the computer algebra system <a href="http://www.sagemath.org/" rel="nofollow">Sage</a>, I need to multiply a list by 2.</p> <p>I tried the code</p> <pre><code>sage: list = [1, 2, 3]; sage: 2 * list </code></pre> <p>which returns</p> <pre><code>[1, 2, 3, 1, 2, 3] </code></pre> <p>How can I just multiply eac...
1
2009-02-06T00:48:23Z
8,599,183
<p>You manipulate lists in Sage just how you would manipulate them in Python because Sage is based on Python. So, read about Python lists and you will learn to do whatever you want with lists in Sage. Here:</p> <p><a href="http://docs.python.org/tutorial/datastructures.html" rel="nofollow">http://docs.python.org/tut...
0
2011-12-22T03:39:36Z
[ "python", "list" ]
How can I manipulate lists?
518,679
<p>In the computer algebra system <a href="http://www.sagemath.org/" rel="nofollow">Sage</a>, I need to multiply a list by 2.</p> <p>I tried the code</p> <pre><code>sage: list = [1, 2, 3]; sage: 2 * list </code></pre> <p>which returns</p> <pre><code>[1, 2, 3, 1, 2, 3] </code></pre> <p>How can I just multiply eac...
1
2009-02-06T00:48:23Z
11,052,812
<p>Or convert the list to a vector first:</p> <pre><code>a = vector([1,2,3]) 2*a </code></pre> <p>which returns</p> <pre><code>(2, 4, 6) </code></pre> <p>Vectors can be used in matrix-multiplication, and have methods which might be useful such as ".dot_product".</p> <p>Btw, it is probably not a good idea in Sage o...
1
2012-06-15T14:37:11Z
[ "python", "list" ]
List/Arrays - Check Dates
518,782
<p>I'm trying to make a program that checks an array to make sure there are four folders with partially same names.</p> <p>So</p> <p>For a date like 0103 (jan 3rd), there should be 0103-1, 0103-2, 0103-3, and 0103-4. Other folders are like 0107-1, 0107-2, 0107-3, 0107-4. How do I go about doing this? I thought about ...
0
2009-02-06T01:35:00Z
518,798
<pre><code>import os def myfunc(date, num): for x in range(1, num+1): filename = str(date) + "-" + str(x) if os.path.exists(filename): print(filename+" exists") else: print(filename+" does not exist") myfunc('0102', 3); </code></pre> <p><em>0102-1 does not exist</e...
3
2009-02-06T01:46:44Z
[ "python", "sorting" ]
List/Arrays - Check Dates
518,782
<p>I'm trying to make a program that checks an array to make sure there are four folders with partially same names.</p> <p>So</p> <p>For a date like 0103 (jan 3rd), there should be 0103-1, 0103-2, 0103-3, and 0103-4. Other folders are like 0107-1, 0107-2, 0107-3, 0107-4. How do I go about doing this? I thought about ...
0
2009-02-06T01:35:00Z
519,196
<p>Here's a naive way to find the largest common leading substring given an array of strings:</p> <pre><code>&gt;&gt;&gt; arr = ['0102-1', '0102-2', '0102-3'] &gt;&gt;&gt; for i in reversed(range(len(arr[0]))): ... for s in arr: ... if not s.startswith(arr[0][:i+1]): ... break ... else: ... ...
0
2009-02-06T05:16:05Z
[ "python", "sorting" ]
Getting OperationalError: FATAL: sorry, too many clients already using psycopg2
519,296
<p>I am getting the error OperationalError: FATAL: sorry, too many clients already when using psycopg2. I am calling the close method on my connection instance after I am done with it. I am not sure what could be causing this, it is my first experience with python and postgresql, but I have a few years experience with ...
3
2009-02-06T06:15:50Z
519,304
<p>This error means what it says, there are too many clients connected to postgreSQL. Are you the only one connected to this database? Are you running a graphical IDE? What method are you using to connect? Are you testing queries at the same time that you running the code? any of these things could be the problem. If y...
4
2009-02-06T06:24:20Z
[ "python", "postgresql", "psycopg2" ]
Getting OperationalError: FATAL: sorry, too many clients already using psycopg2
519,296
<p>I am getting the error OperationalError: FATAL: sorry, too many clients already when using psycopg2. I am calling the close method on my connection instance after I am done with it. I am not sure what could be causing this, it is my first experience with python and postgresql, but I have a few years experience with ...
3
2009-02-06T06:15:50Z
15,046,529
<p>Make sure your db connection command isn't in any kind of loop. I was getting the same error from my script until I moved my db.database() out of my programs repeating execution loop.</p>
1
2013-02-23T22:34:39Z
[ "python", "postgresql", "psycopg2" ]
Lazy Method for Reading Big File in Python?
519,633
<p>I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into another file and read next piece.</p> <p>Is there any method to <code>yield</code> these pieces ?</p> <p>I would love to have a <strong>lazy met...
156
2009-02-06T09:11:13Z
519,653
<p>To write a lazy function, just use <a href="http://docs.python.org/tutorial/classes.html#generators"><code>yield</code></a>:</p> <pre><code>def read_in_chunks(file_object, chunk_size=1024): """Lazy function (generator) to read a file piece by piece. Default chunk size: 1k.""" while True: data = ...
231
2009-02-06T09:20:08Z
[ "python", "file-io", "generator" ]
Lazy Method for Reading Big File in Python?
519,633
<p>I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into another file and read next piece.</p> <p>Is there any method to <code>yield</code> these pieces ?</p> <p>I would love to have a <strong>lazy met...
156
2009-02-06T09:11:13Z
519,667
<p>Take a look at <a href="http://neopythonic.blogspot.com/2008/10/sorting-million-32-bit-integers-in-2mb.html">this post on Neopythonic</a>: "Sorting a million 32-bit integers in 2MB of RAM using Python"</p>
18
2009-02-06T09:28:12Z
[ "python", "file-io", "generator" ]
Lazy Method for Reading Big File in Python?
519,633
<p>I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into another file and read next piece.</p> <p>Is there any method to <code>yield</code> these pieces ?</p> <p>I would love to have a <strong>lazy met...
156
2009-02-06T09:11:13Z
519,693
<p>You can use the <a href="http://docs.python.org/library/mmap.html">mmap module</a> to map the contents of the file into memory and access it with indices and slices. Here an example from the documentation:</p> <pre><code>import mmap with open("hello.txt", "r+") as f: # memory-map the file, size 0 means whole fi...
20
2009-02-06T09:41:29Z
[ "python", "file-io", "generator" ]
Lazy Method for Reading Big File in Python?
519,633
<p>I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into another file and read next piece.</p> <p>Is there any method to <code>yield</code> these pieces ?</p> <p>I would love to have a <strong>lazy met...
156
2009-02-06T09:11:13Z
519,770
<p>I'm in a somewhat similar situation. It's not clear whether you know chunk size in bytes; I usually don't, but the number of records (lines) that is required is known:</p> <pre><code>def get_line(): with open('4gb_file') as file: for i in file: yield i lines_required = 100 gen = get_line...
1
2009-02-06T10:12:47Z
[ "python", "file-io", "generator" ]
Lazy Method for Reading Big File in Python?
519,633
<p>I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into another file and read next piece.</p> <p>Is there any method to <code>yield</code> these pieces ?</p> <p>I would love to have a <strong>lazy met...
156
2009-02-06T09:11:13Z
519,818
<p>i am not allowed to comment due to my low reputation, but SilentGhosts solution should be much easier with file.readlines([sizehint])</p> <p><a href="http://docs.python.org/library/stdtypes.html#file-objects" rel="nofollow">python file methods</a></p> <p>edit: SilentGhost is right, but this should be better than:<...
1
2009-02-06T10:37:22Z
[ "python", "file-io", "generator" ]
Lazy Method for Reading Big File in Python?
519,633
<p>I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into another file and read next piece.</p> <p>Is there any method to <code>yield</code> these pieces ?</p> <p>I would love to have a <strong>lazy met...
156
2009-02-06T09:11:13Z
2,111,801
<p>file.readlines() takes in an optional size argument which approximates the number of lines read in the lines returned.</p> <pre><code>bigfile = open('bigfilename','r') tmp_lines = bigfile.readlines(BUF_SIZE) while tmp_lines: process([line for line in tmp_lines]) tmp_lines = bigfile.readlines(BUF_SIZE) </cod...
18
2010-01-21T18:27:59Z
[ "python", "file-io", "generator" ]
Lazy Method for Reading Big File in Python?
519,633
<p>I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into another file and read next piece.</p> <p>Is there any method to <code>yield</code> these pieces ?</p> <p>I would love to have a <strong>lazy met...
156
2009-02-06T09:11:13Z
9,952,333
<pre><code>f = ... # file-like object, i.e. supporting read(size) function and # returning empty string '' when there is nothing to read def chunked(file, chunk_size): return iter(lambda: file.read(chunk_size), '') for data in chunked(f, 65536): # process the data </code></pre> <p>UPDATE: The approa...
6
2012-03-31T01:50:18Z
[ "python", "file-io", "generator" ]
Lazy Method for Reading Big File in Python?
519,633
<p>I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into another file and read next piece.</p> <p>Is there any method to <code>yield</code> these pieces ?</p> <p>I would love to have a <strong>lazy met...
156
2009-02-06T09:11:13Z
10,405,648
<p>To process line by line, this is an elegant solution:</p> <pre><code> def stream_lines(file_name): file = open(file_name) while True: line = file.readline() if not line: file.close() break yield line </code></pre> <p>As long as there're no blank lines.</p>
0
2012-05-01T23:12:15Z
[ "python", "file-io", "generator" ]
Lazy Method for Reading Big File in Python?
519,633
<p>I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into another file and read next piece.</p> <p>Is there any method to <code>yield</code> these pieces ?</p> <p>I would love to have a <strong>lazy met...
156
2009-02-06T09:11:13Z
19,802,839
<p>I think we can write like this:</p> <pre><code>def read_file(path, block_size=1024): with open(path, 'rb') as f: while True: piece = f.read(block_size) if piece: yield piece else: return for piece in read_file(path): process...
1
2013-11-06T02:15:10Z
[ "python", "file-io", "generator" ]
Lazy Method for Reading Big File in Python?
519,633
<p>I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into another file and read next piece.</p> <p>Is there any method to <code>yield</code> these pieces ?</p> <p>I would love to have a <strong>lazy met...
156
2009-02-06T09:11:13Z
30,775,393
<p>There are already many good answers, but I ran into a similar issue recently and the solution I needed is not listed here, so I figured I could complement this thread.</p> <p>80% of the time, I need to read files line by line. Then, as suggested in this <a href="http://stackoverflow.com/a/519653/628786">answer</a>,...
3
2015-06-11T08:23:16Z
[ "python", "file-io", "generator" ]
Lazy Method for Reading Big File in Python?
519,633
<p>I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into another file and read next piece.</p> <p>Is there any method to <code>yield</code> these pieces ?</p> <p>I would love to have a <strong>lazy met...
156
2009-02-06T09:11:13Z
30,916,544
<p>you can use following code.</p> <pre><code>file_obj = open('big_file') </code></pre> <p>open() returns a file object</p> <p>then use os.stat for getting size</p> <pre><code>file_size = os.stat('big_file').st_size for i in range( file_size/1024): print file_obj.read(1024) </code></pre>
0
2015-06-18T13:20:52Z
[ "python", "file-io", "generator" ]
Is there a HAML implementation for use with Python and Django
519,671
<p>I happened to stumble across <a href="http://haml-lang.com/">HAML</a>, an interesting and beautiful way to mark up contents and write templates for HTML.</p> <p>Since I use Python and Django for my web developing need, I would like to see if there is a Python implementation of HAML (or some similar concepts -- need...
68
2009-02-06T09:30:01Z
519,679
<p>I'd check out <a href="https://github.com/derdon/ghrml">GHRML</a>, Haml for Genshi. The author admits that it's basically Haml for Python and that most of the syntax is the same (and that it works in Django). Here's some GHRML just to show you how close they are:</p> <pre><code>%html %head %title Hello World ...
21
2009-02-06T09:36:46Z
[ "python", "django", "django-templates", "haml" ]
Is there a HAML implementation for use with Python and Django
519,671
<p>I happened to stumble across <a href="http://haml-lang.com/">HAML</a>, an interesting and beautiful way to mark up contents and write templates for HTML.</p> <p>Since I use Python and Django for my web developing need, I would like to see if there is a Python implementation of HAML (or some similar concepts -- need...
68
2009-02-06T09:30:01Z
1,582,702
<p>This doesn't actually answer your question, but the CSS component of HAML, <a href="http://sass-lang.com/" rel="nofollow">SASS</a>, can be used freely with any framework. I'm using it right now with Django. </p>
4
2009-10-17T17:18:24Z
[ "python", "django", "django-templates", "haml" ]
Is there a HAML implementation for use with Python and Django
519,671
<p>I happened to stumble across <a href="http://haml-lang.com/">HAML</a>, an interesting and beautiful way to mark up contents and write templates for HTML.</p> <p>Since I use Python and Django for my web developing need, I would like to see if there is a Python implementation of HAML (or some similar concepts -- need...
68
2009-02-06T09:30:01Z
1,934,207
<p>You might be interested in SHPAML:</p> <p><a href="http://shpaml.com/">http://shpaml.com/</a></p> <p>I am actively maintaining it. It is a simple preprocessor, so it is not tied to any other tools like Genshi. I happen to use it with Django, so there is a little bit of Django support, but it should not interfere...
35
2009-12-19T21:33:50Z
[ "python", "django", "django-templates", "haml" ]
Is there a HAML implementation for use with Python and Django
519,671
<p>I happened to stumble across <a href="http://haml-lang.com/">HAML</a>, an interesting and beautiful way to mark up contents and write templates for HTML.</p> <p>Since I use Python and Django for my web developing need, I would like to see if there is a Python implementation of HAML (or some similar concepts -- need...
68
2009-02-06T09:30:01Z
2,765,628
<p>I'm not sure what the status is of the GHRML bit as I only recently was looking into it. Can't find a repo for it, original developer doesn't have time for it anymore and maintenance was picked up by someone else with an interest in the project. Any extra info on this would be helpful.</p> <p>Unfortunately, as thes...
3
2010-05-04T13:22:42Z
[ "python", "django", "django-templates", "haml" ]
Is there a HAML implementation for use with Python and Django
519,671
<p>I happened to stumble across <a href="http://haml-lang.com/">HAML</a>, an interesting and beautiful way to mark up contents and write templates for HTML.</p> <p>Since I use Python and Django for my web developing need, I would like to see if there is a Python implementation of HAML (or some similar concepts -- need...
68
2009-02-06T09:30:01Z
3,324,983
<p>i'm looking for the same. I haven't tried it, but found this:</p> <p><a href="http://github.com/jessemiller/HamlPy">http://github.com/jessemiller/HamlPy</a></p>
18
2010-07-24T11:56:45Z
[ "python", "django", "django-templates", "haml" ]