XML的有效性的检测

XML文档的结构良好性验证,结构良好的xml文档,需要遵守下面这些规则:

  1. 所有开始标签必须有对应的结束标签
  2. 元素可以嵌套,但是不可以重叠
  3. 有且只能有一个根元素
  4. 属性值必须使用引号
  5. 一个元素不能有两个同样属性名字的属性
  6. 注释不能出现在标签内部
  7. 没有转义的” <” 或者” $”不能出现在元素和属性的字符中

如何检查XML文档良构?

Read more

Python批量删除特定后缀名的文件和目录

利用 Python 在日常工作中,删除指定目录已经子目录下的特定后缀的文件名。

Python Version: 2.7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# coding:utf-8
import os
import sys
import os
import shutil

#获取当前路径
def fileDir() :
path = sys.path[ 0 ]
print(path)
#判断为脚本文件还是编译后文件,如果是脚本文件则返回脚本目录,否则返回编件译后的文件路径
if os.path.isdir( path ) :
return path
elif os.path.isfile( path ) :
return os.path.dirname( path )

#获取文件后缀名
def suffix(fileName, *suffixName) :
array = map(fileName.endswith, suffixName)
if True in array :
return True
else :
return False

#删除目录下扩展名为.exe,.bak的文件
def deleteFile() :
target_dir = fileDir()
for root, dir_names, file_names in os.walk(target_dir):
for file in file_names:
target_file = os.path.join(root, file)
if suffix(file, '.doc', '.xls'):
os.remove(target_file)
# 文件夹名字
if file == 'a':
shutil.rmtree(os.path.join(root, dir_names))


if __name__ == '__main__' :
deleteFile()

pyinstaller教程

简介:PyInstaller可以用来打包python应用程序,打包完的程序就可以在没有安装Python解释器的机器上运行了。PyInstaller支持Python 2.7和Python 3.3+。可以在Windows、Mac OS X和Linux上使用,但是并不是跨平台的,而是说你要是希望打包成.exe文件,需要在Windows系统上运行PyInstaller进行打包工作;打包成mac app,需要在Mac OS上使用。

Read more

Python的最大递归深度错误

今天在用 Python 的爬虫的时候,遇到一个错误maximum recursion depth exceeded while calling a Python object,意思是:当调用该对象超过最大递归深度。

报错如下:

Read more

Python ConfigParser模块常用方法示例

在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在Python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介绍。

Python ConfigParser模块解析的配置文件的格式比较象ini的配置文件格式,就是文件中由多个section构成,每个section下又有多个配置项.

Read more

快速升级 Python 的模块

pip 当前内建命令并不支持升级所有已安装的Python模块,所有使用命令行来查询和升级 Python 的模块

列出当前安装的包:
1
pip list
列出可升级的包:
1
pip list --outdate
Read more

Python 字符编码

字符串也是一种数据类型,但比较特殊的是字符串存在一个编码问题,也就是我们怎么用计算机来表示相应的字符并存储。在编程语言中,我们经常会处理文本编码之间的转化问题,因为文本可能存在不同的编码,比如 ASCII、GBK、UTF-8 等等。

Read more

模块zlib 压缩与解压

模块zlib用来解压和压缩字符串或者文件,能够自动识别压缩格式来自动解压。

字符串的解压与字符串:

Read more

Python递归实现字典中的Unicode转换成str

由于josn库的loads方法会把类型全部转换成 Unicode。如果想要变成str对象的话,就要自己去encode。

试试如下代码:

1
2
3
4
5
6
7
8
9
def byteify(input):
if isinstance(input, dict):
return {byteify(key): byteify(value) for key, value in input.iteritems()}
elif isinstance(input, list):
return [byteify(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input

Python 列出当前目录的文件和文件夹

只获取当前目录的文件和文件夹

1
2
3
4
5
6
7
list = os.listdir(rootdir)#列出目录下的所有文件和目录
for line in list:
filepath = os.path.join(rootdir,line)
if os.path.isdir(filepath):#如果filepath是目录
print "dir:" + filepath
else:
print "file:" + filepath
Read more
Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×