Python语言总结 4.2. 和字符串(str,unicode等)处理有关的函数
Python语言总结
4.2. 和字符串(str,unicode等)处理有关的函数
Sidebar Prev | Up | Next
4.2.7. 去除控制字符:removeCtlChr使得处理后的字符串,在XML都是合法的了。
#------------------------------------------------------------------------------
# remove control character from input string
# otherwise will cause wordpress importer import failed
# for wordpress importer, if contains contrl char, will fail to import wxr
# eg:
# 1. http://againinput4.blog.163.com/blog/static/172799491201110111145259/
# content contains some invalid ascii control chars
# 2. http://hi.baidu.com/notebookrelated/blog/item/8bd88e351d449789a71e12c2.html
# 165th comment contains invalid control char: ETX
# 3. http://green-waste.blog.163.com/blog/static/32677678200879111913911/
# title contains control char:DC1, BS, DLE, DLE, DLE, DC1
def removeCtlChr(inputString) :
validContent = '';
for c in inputString :
asciiVal = ord(c);
validChrList = [
9, # 9=\t=tab
10, # 10=\n=LF=Line Feed=换行
13, # 13=\r=CR=回车
];
# filter out others ASCII control character, and DEL=delete
isValidChr = True;
if (asciiVal == 0x7F) :
isValidChr = False;
elif ((asciiVal < 32) and (asciiVal not in validChrList)) :
isValidChr = False;
if(isValidChr) :
validContent += c;return validContent;
Example 4.11. removeCtlChr的使用范例
# remove the control char in title:
# eg;
# http://green-waste.blog.163.com/blog/static/32677678200879111913911/
# title contains control char:DC1, BS, DLE, DLE, DLE, DC1
infoDict['title'] = removeCtlChr(infoDict['title']);[Tip] 关于控制字符
如果不了解什么是控制字符,请参考:ASCII字符集中的功能/控制字符
Prev Up Next
4.2.6. 去除非单词(non-word)的字符:removeNonWordChar Home 4.2.8. 将字符实体替换为Unicode数字实体:replaceStrEntToNumEntContents
Searchloading table of contents...
SearchSearch Highlighter (On/Off)
Python语言总结 4.2. 和字符串(str,unicode等)处理有关的函数的更多相关文章
- javaScript 字符串与unicode码之间的相互转换,函数的封装
在我们的开发过程中,有时在对数据进行储存的时候,我们需要将字符串转成unicode. 比如,在jsp开发时,前端使用页面间传值时,将传值参数先存入cookie中,然后在使用的时候,再从ookie中取出 ...
- Python语言程序设计(3)--字符串类型及操作--time库进度条
1.字符串类型的表示: 三引号可做注释,注释其实也是字符串 2.字符串的操作符 3.字符串处理函数 输出:
- Python 语言来编码和解码 JSON 对象
Json函数: json.dumps: Python标准库中的json模块,集成了将数据序列化处理的功能. 将 Python 对象编码成 JSON 字符串 语法: json.dumps(obj, sk ...
- [Python学习笔记1]Python语言基础 数学运算符 字符串 列表
这个系列是我在学习Python语言的过程中记录的笔记,主要是一些知识点汇总,而非学习教程,可供有一定编程基础者参考.文中偏见和不足难以避免,仅供参考,欢迎批评指正. 本系列笔记主要参考文献是官网文档: ...
- 人生苦短之我用Python篇(列表list、字典dict、元组tuple、字符串str)
列表 创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_s ...
- Python内置数据结构之字符串str
1. 数据结构回顾 所有标准序列操作(索引.切片.乘法.成员资格检查.长度.最小值和最大值)都适用于字符串,但是字符串是不可变序列,因此所有的元素赋值和切片赋值都是非法的. >>> ...
- Python字符串str的方法使用
#!usr/bin/env python# -*-coding:utf-8-*-#字符串通常用双引号或单引号来表示:'123',"abc","字符串"#str字 ...
- 1.3 正则表达式和python语言-1.3.4使用 match()方法匹配字符串
1.3.4使用 match()方法匹配字符串(第一次写博客,格式,述语有不当之处还请见谅)2018-05-08 Python 代码是以Jupyter Notebook编写的,主要写的是python3的 ...
- python中字符串(str)的常用处理方法
str='python String function' 生成字符串变量str='python String function' 字符串长度获取:len(str)例:print '%s length= ...
随机推荐
- CString Format 乱码问题
CString m_buf;CStatic *m_static;char *szName;...m_buf.Format(":%s",szName);m_static->Se ...
- OCP-1Z0-042-V12.39-47题
47.Which two database operations can be performed at the mount stage of database startup? 题目解析: A和E在 ...
- 搭建Go开发及调试环境(LiteIDE + GoClipse)
搭建Go开发及调试环境(LiteIDE + GoClipse) -- Windows篇 这里以Windows7 64位为例,如果是32位环境需安装对应版本程序. 一.安装golang1.2.2 1.3 ...
- iframe间的通信
父框架 <body></body> <script type="text/javascript"> document.domain = '100 ...
- Windows下实战Apache+PHP [转]
一.Apache 1.下载登陆Apache Lougne(http://www.apachelounge.com/download/),找到最新版本的Apache.笔者下载的是带IPv6和Cr ...
- 基于visual Studio2013解决C语言竞赛题之1082迷宫
题目 解决代码及点评 /************************************************************************/ /* ...
- paip.php 配置ZEND DEBUGGER 断点调试for cli..
paip.php 配置ZENDDEBUGGER 断点调试for cli.. 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http:/ ...
- 【实战】静默安装-oracle 11.2.0.3 on centos 5.10
发现网上静默安装的文章非常多,乱七八糟,五花八门!来个扫盲的! centos 5.10 下安装oracle 11g_r2 ************************************* ...
- 【Demo 0005】Android 资源
本章学习要点: 1. 了解Android中资源用途: 2. 掌握资源使用通用规则: 3. 掌握具体资源使用方法; 一.Android资源 a ...
- TDD测试驱动的javascript开发(3) ------ javascript的继承
说起面向对象,人们就会想到继承,常见的继承分为2种:接口继承和实现继承.接口继承只继承方法签名,实现继承则继承实际的方法. 由于函数没有签名,在ECMAScript中无法实现接口继承,只支持实现继承. ...