python-2.x

Python 2.x multiple version issues regarding PYTHONPATH

不想你离开。 提交于 2019-12-18 17:00:02
问题 There's Python 2.6 installed in the system. Now I want to use modules introduced in Python 2.7. Because I have no root privilege, I have built & installed 2.7 under my home directory ($HOME/local/) I added the following to my $HOME/.bashrc: export PATH=$HOME/local/bin:$PATH export PYTHONPATH=$HOME/local/lib/python2.7:$PYTHONPATH Now I encountered the two problems I want ask for workarounds. 1. Invoking Python 2.7 Newly installed Python 2.7 doesn't find 2.6 modules in system's library path (

Valid syntax in both Python 2.x and 3.x for raising exception?

為{幸葍}努か 提交于 2019-12-18 13:18:42
问题 How can I port this code to Python 3 so that it would run in both, Python 2 and Python3? raise BarException, BarException(e), sys.exc_info()[2] (copied from http://blog.ionelmc.ro/2014/08/03/the-most-underrated-feature-in-python-3/) Bonus question Does it make sense to do something like IS_PYTHON2 = sys.version_info < (3, 0) if IS_PYTHON2: raise BarException, BarException(e), sys.exc_info()[2] # replace with the code that would run in Python 2 and Python 3 respectively else: raise

Python: Check if a string contains chinese character?

做~自己de王妃 提交于 2019-12-18 11:55:35
问题 A string maybe this ipath= "./data/NCDC/上海/虹桥/9705626661750dat.txt" or this ipath = './data/NCDC/ciampino/6240476818161dat.txt' How do I know the first string contains chinese ? I find this answer maybe helpful: Find all Chinese text in a string using Python and Regex but it didn't work out: import re ipath= "./data/NCDC/上海/虹桥/9705626661750dat.txt" re.findall(ur'[\u4e00-\u9fff]+', ipath) # => [] 回答1: The matched string should be unicode as well >>> import re >>> ipath= u"./data/NCDC/上海/虹桥

Why does Python's dict.keys() return a list and not a set?

戏子无情 提交于 2019-12-18 11:14:57
问题 I would've expected Python's keys method to return a set instead of a list. Since it most closely resembles the kind of guarantees that keys of a hashmap would give. Specifically, they are unique and not sorted, like a set. However, this method returns a list: >>> d = {} >>> d.keys().__class__ <type 'list'> Is this just a mistake in the Python API or is there some other reason I am missing? 回答1: One reason is that dict.keys() predates the introduction of sets into the language. Note that the

Tutorial for Python - Should I use 2.x or 3.0? [closed]

跟風遠走 提交于 2019-12-18 05:54:55
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Python 3.0 is in beta with a final release coming shortly. Obviously it will take some significant time for general adoption and for

matplotlib color map - predefine mappings to values?

让人想犯罪 __ 提交于 2019-12-18 05:18:08
问题 I have an array that I am viewing using imshow() . (imsave() really, but the process should be identical). I know that the values in the array will be between 0-9 and wonder if it is possible to use cmap to set each output to a specific 'color'. Perhaps by mapping these to a dict? 回答1: Just use a ListedColormap . As a quick (but ugly) example: import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap cmap = ListedColormap(['red', 'green', 'blue', 'black'], 'indexed') fig,

Many threads to write log file at same time in Python

人走茶凉 提交于 2019-12-18 04:48:16
问题 I am writing a script to retrieve WMI info from many computers at the same time then write this info in a text file: f = open("results.txt", 'w+') ## to clean the results file before the start def filesize(asset): f = open("results.txt", 'a+') c = wmi.WMI(asset) wql = 'SELECT FileSize,Name FROM CIM_DataFile where (Drive="D:" OR Drive="E:") and Caption like "%file%"' for item in c.query(wql): print >> f, item.Name.split("\\")[2].strip().upper(), str(item.FileSize) class myThread (threading

Why does id() of an unbound method in Python 2 change for every access

て烟熏妆下的殇ゞ 提交于 2019-12-18 04:12:43
问题 Python 2.6.5 (r265:79063, Oct 1 2012, 22:07:21) [GCC 4.4.3] >>> class myclass: ... def func(self): ... pass >>> dd = myclass.func >>> ee = myclass.func >>> cc = myclass.func >>> ff = myclass.func >>> ss = myclass.func >>> uu = myclass.func >>> pp = myclass.func >>> >>> >>> id(dd) ; id(cc) ; id(ee) ; id(ff) ; id(ss) ; id(uu) ; id(pp) 3074535252L 3074534772L 3074522444L 3074531732L 3074497588L 3073003604L 3073003724L Why is the ID of the unbound method different each time? Shouldn't it be same

Correctly extract Emojis from a Unicode string

六眼飞鱼酱① 提交于 2019-12-18 01:56:17
问题 I am working in Python 2 and I have a string containing emojis as well as other unicode characters. I need to convert it to a list where each entry in the list is a single character/emoji. x = u'😘😘xyz😊😊' char_list = [c for c in x] The desired output is: ['😘', '😘', 'x', 'y', 'z', '😊', '😊'] The actual output is: [u'\ud83d', u'\ude18', u'\ud83d', u'\ude18', u'x', u'y', u'z', u'\ud83d', u'\ude0a', u'\ud83d', u'\ude0a'] How can I achieve the desired output? 回答1: First of all, in Python2, you need

In Python 2, how do I write to variable in the parent scope?

不羁的心 提交于 2019-12-17 17:33:22
问题 I have the following code inside a function: stored_blocks = {} def replace_blocks(m): block = m.group(0) block_hash = sha1(block) stored_blocks[block_hash] = block return '{{{%s}}}' % block_hash num_converted = 0 def convert_variables(m): name = m.group(1) num_converted += 1 return '<%%= %s %%>' % name fixed = MATCH_DECLARE_NEW.sub('', template) fixed = MATCH_PYTHON_BLOCK.sub(replace_blocks, fixed) fixed = MATCH_FORMAT.sub(convert_variables, fixed) Adding elements to stored_blocks works fine