python-2.6

Replace html entities with the corresponding utf-8 characters in Python 2.6

元气小坏坏 提交于 2019-12-03 17:57:58
问题 I have a html text like this: <xml ... > and I want to convert it to something readable: <xml ...> Any easy (and fast) way to do it in Python? 回答1: Python 2.7 Official documentation for HTMLParser : Python 2.7 >>> import HTMLParser >>> pars = HTMLParser.HTMLParser() >>> pars.unescape('© €') u'\xa9 \u20ac' >>> print _ © € Python 3 Official documentation for HTMLParser : Python 3 >>> from html.parser import HTMLParser >>> pars = HTMLParser() >>> pars.unescape('© €') © € 回答2: There is a function

How to get in python the key pressed without press enter?

喜你入骨 提交于 2019-12-03 17:31:15
I saw here a solution, but i don't want wait until the key is pressed. I want to get the last key pressed. jsbueno The related question may help you, as @S.Lott mentioned: Detect in python which keys are pressed I am writting in, though to give yu advice: don't worry about that. What kind of program are you trying to produce? Programas running on a terminal usually don't have an interface in which getting "live" keystrokes is interesting. Not nowadays. For programs running in the terminal, you should worry about a usefull command line User Interfase, using the optparse or other modules. For

Reading/writing to a Popen() subprocess

守給你的承諾、 提交于 2019-12-03 17:07:38
问题 I'm trying to talk to a child process using the python subprocess.Popen() call. In my real code, I'm implementing a type of IPC, so I want to write some data, read the response, write some more data, read the response, and so on. Because of this, I cannot use Popen.communicate(), which otherwise works well for the simple case. This code shows my problem. It never even gets the first response, hangs at the first "Reading result". Why? How can I make this work as I expect? import subprocess p =

Using Google App Engine SDK with Python 2.7 on Mac OS X 10.6

↘锁芯ラ 提交于 2019-12-03 07:15:51
问题 I need to run Python 2.7 on my Mac Snow Leopard, which has Python 2.6 installed. According to this answer, running the Python 2.7 mpkg installer from Python.org should get me there. The reason I need to do this is that I'm trying to run the Google App Engine SDK for the Python 2.7 runtime. After installing Python 2.7, I'm still getting the following warning in my GAE server log: Warning: You are using a Python runtime (2.6) that is older than the production runtime environment (2.7). What

Reading/writing to a Popen() subprocess

我是研究僧i 提交于 2019-12-03 06:52:09
I'm trying to talk to a child process using the python subprocess.Popen() call. In my real code, I'm implementing a type of IPC, so I want to write some data, read the response, write some more data, read the response, and so on. Because of this, I cannot use Popen.communicate(), which otherwise works well for the simple case. This code shows my problem. It never even gets the first response, hangs at the first "Reading result". Why? How can I make this work as I expect? import subprocess p = subprocess.Popen(["sed", 's/a/x/g'], stdout = subprocess.PIPE, stdin = subprocess.PIPE) p.stdin.write(

How to get the current running module path/name

天大地大妈咪最大 提交于 2019-12-03 06:38:11
问题 I've searched and this seems to be a simple question without a simple answer. I have the file a/b/c.py which would be called with python -m a.b.c . I would like to obtain the value a.b.c in the module level. USAGE = u'''\ Usage: python -m %s -h ''' % (what_do_i_put_here,) So when I receive the -h option, I display the USAGE without the need to actually write down the actual value in each and every script. Do I really need to go through inspect to get the desired value? Thanks. EDIT: As said,

“outsourcing” exception-handling to a decorator [closed]

夙愿已清 提交于 2019-12-03 05:26:21
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . Many try/except/finally-clauses not only "uglify" my code, but i find myself often using identical exception-handling for similar tasks. So i was considering reducing redundancy by "outsourcing" them to a ... decorator. Because i was sure not to be the 1st one to come to this

How to fix symbol lookup error: undefined symbol errors in a cluster environment

守給你的承諾、 提交于 2019-12-03 04:12:09
问题 I'm working on some python code that extracts some image data from an ECW file using GDAL (http://www.gdal.org/) and its python bindings. GDAL was built from source to have ECW support. The program is run on a cluster server that I ssh into. I have tested the program through the ssh terminal and it runs fine. However, I would now like to submit a job to the cluster using qsub, but it reports the following: Traceback (most recent call last): File "./gdal-test.py", line 5, in <module> from

Why does Python have a format function as well as a format method

烂漫一生 提交于 2019-12-03 04:08:30
问题 The format function in builtins seems to be like a subset of the str.format method used specifically for the case of a formatting a single object. eg. >>> format(13, 'x') 'd' is apparently preferred over >>> '{0:x}'.format(13) 'd' and IMO it does look nicer, but why not just use str.format in every case to make things simpler? Both of these were introduced in 2.6 so there must be a good reason for having both at once, what is it? Edit: I was asking about str.format and format , not why we don

Can bin() be overloaded like oct() and hex() in Python 2.6?

只愿长相守 提交于 2019-12-03 02:05:52
In Python 2.6 (and earlier) the hex() and oct() built-in functions can be overloaded in a class by defining __hex__ and __oct__ special functions. However there is not a __bin__ special function for overloading the behaviour of Python 2.6's new bin() built-in function. I want to know if there is any way of flexibly overloading bin() , and if not I was wondering why the inconsistent interface? I do know that the __index__ special function can be used, but this isn't flexible as it can only return an integer. My particular use case is from the bitstring module, where leading zero bits are