Linux下安装python3

Categories: Python
export LANG=zh_CN.UTF-8 export LANGUAGE=zh_CN.UTF-8 make之前需要检查openssl-devel

cdll windll oledll

Categories: Python
cdll windll oledll 它们的不同之处在于:动态链接库中的函数所遵守的函数调用方式(calling convention)以及返回方式有所不同。 cdll用于加载遵循cdecl调用约定的动态链接库,windll用于加载遵循stdcall调用约定的动态链接库,oledll与windll完全相同,只是会默认其载入的函数统一返回一个Windows HRESULT错误编码。

Decode HTML entities

Categories: Python
Python3.4+ import html print(html.unescape('£682m')) Python2.6-3.3 from html.parser import HTMLParser h = HTMLParser() print(h.unescape('£682m')) link

Python3自定义排序

Categories: Python
1. 笨方法,不推荐 a = ['星期一', '星期三', '星期二', '星期日'] c = {'星期一': 1, '星期二': 2, '星期三': 3, '星期四': 4, '星期五': 5, '星期六': 6, '星期日': 7} b = {} for i in a: b[i] = c[i] b = sorted(b.items(), key=lambda t: t[1]) # b = sorted(zip(b.values(), b.keys())) # 使用zip下面改为j[1] a = [] for j in b: day = j[0] a.append(day) print(a) 2. 自定义大小关系 python3中取消comparison function,使用key function,cmp_to_key帮助cmp过渡为key def compare_day(day1, day2): c = {'星期一': 1, '星期二': 2, '星期三': 3, '星期四': 4, '星期五': 5, '星期六': 6, '星期日': 7} value1 = c[day1] value2 = c[day2] if value1 > value2: return 1 if value1 < value2: return -1 return 0 a = ['星期一', '星期三', '星期二', '星期日'] from functools import cmp_to_key a.

Read More →

XML解析

Categories: Python
XML # document.xml为docx文件中的 from xml.etree.ElementTree import parse from xml.etree.ElementTree import XMLParser f = open('document.xml') doc = parse(f, XMLParser(encoding="utf-8")) # 1 t_elems = doc.findall('.//{http://schemas.openxmlformats.org/wordprocessingml/2006/main}t') # 2 ns = {'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'} t_elems = doc.findall('.//w:t', ns) # 3 t_elems = doc.iter('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}t') for elem in t_elems: print(elem.text) LXML from lxml import etree f = open('document.xml') doc_lxml = etree.parse(f, etree.XMLParser(encoding="utf-8")) # 1 p_lxml = doc_lxml.iter('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}t') # 2 ns = {'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'} find_results = doc_lxml.findall('//w:t', ns) # Namespace prefix->URI mapping known in the context of this Element.

Read More →

lxml

Categories: Python
from lxml import etree html = etree.HTML(resHtml, parser=etree.HTMLParser(encoding='utf-8')) # 处理源文件的时候,由于没有指定编码,所以它使用了一个默认编码,从而导致和UTF-8冲突,产生乱码 # http://lxml.

Read More →

undefined symbol PyUnicodeUCS2_FromUnicode

Categories: Python
import sys print(sys.maxunicode) # 大于65535为UCS4,否则为UCS2 python2.6.6 – 1114111 python2.7.10 – 65535 python2.7.13 – 65535 python3.5.2 – 1114111 UCS4为使用4字节Unicode编译扩展模块,UCS2为2字节Unicode,解决方法是使用对应的python版本编译