【python – 将’10yrs 5mon’分类值转换为月份】教程文章相关的互联网学习教程文章

Python实现把回车符rn转换成n

最近在做cocos2d-x的简明配置,发现有的朋友的文本编辑器,自动将\r\n截断成\n,(在unix上换行使用\n,windows上,换行使用的是\r\n)于是,写了这个脚本,希望对一些朋友有所帮助,不用一行一行去改import osdef replace(filePath, w2u):try:oldfile = open(filePath, "rb+") #这里必须用b打开path, name = os.path.split(filePath)newfile = open(path + $ + name, "ba+")old = bnew = bif w2u == True:old = b\rnew = bels...

Python中实现对Timestamp和Datetime及UTC时间之间的转换

Python项目中很多时候会需要将时间在Datetime格式和TimeStamp格式之间转化,又或者你需要将UTC时间转化为本地时间,本文总结了这几个时间之间转化的函数,供大家参考。 一、Datetime转化为TimeStampdef datetime2timestamp(dt, convert_to_utc=False): Converts a datetime object to UNIX timestamp in milliseconds. if isinstance(dt, datetime.datetime):if convert_to_utc: # 是否转化为UTC时间dt = dt + datetime.timedelta(...

python使用PythonMagick将jpg图片转换成ico图片的方法

本文实例讲述了python使用PythonMagick将jpg图片转换成ico图片的方法。分享给大家供大家参考。具体分析如下: 这里使用到了PythonMagick模块,关于PythonMagick模块和ImageMagick的详细信息请参考:http://www.imagemagick.org/。 下面这段代码可以讲jpg图片转换成ico图标格式。# -*- coding: utf-8 -*- import PythonMagick img = PythonMagick.Image("c:/1.jpg") img.sample(128x128) img.write(c:/1.ico)希望本文所述对大家的P...

python将图片文件转换成base64编码的方法【图】

本文实例讲述了python将图片文件转换成base64编码的方法。分享给大家供大家参考。具体实现方法如下:import base64 f=open(rc:\jb51.gif,rb) #二进制方式打开图文件 ls_f=base64.b64encode(f.read()) #读取文件内容,转换为base64编码 f.close()调用方法如下:代码如下: 希望本文所述对大家的Python程序设计有所帮助。

python通过pil将图片转换成黑白效果的方法

本文实例讲述了python通过pil将图片转换成黑白效果的方法。分享给大家供大家参考。具体分析如下: pil功能强大,convert方法可以轻易的将图片转换,下面的代码可以将图片转换成黑白效果from PIL import Image image_file = Image.open("convert_image.png") # open colour image image_file = image_file.convert(1) # convert image to black and white image_file.save(result.png)希望本文所述对大家的Python程序设计有所帮助。

Python写的英文字符大小写转换代码示例

几行代码的小工具,用于进行如下转换 TRANSACTIONS ON CLOUD COMPUTING =》 Transactions On Cloud Computing代码如下: orig = TRANSACTIONS ON CLOUD COMPUTING splited = orig.split( ) handled = for word in splited:word = word[0] + word[1:].lower()handled += ( + word) handled = handled[1:] print handled #handled is Transactions On Cloud Computing

Python实现把utf-8格式的文件转换成gbk格式的文件

需求:将utf-8格式的文件转换成gbk格式的文件 实现代码如下:代码如下: def ReadFile(filePath,encoding="utf-8"):with codecs.open(filePath,"r",encoding) as f:return f.read() def WriteFile(filePath,u,encoding="gbk"):with codecs.open(filePath,"w",encoding) as f:f.write(u) def UTF8_2_GBK(src,dst):content = ReadFile(src,encoding="utf-8")WriteFile(dst,content,encoding="gbk")代码讲解: 函数ReadFile的第二个参数...

Python判断字符串与大小写转换

判断字符串s.isalnum() #所有字符都是数字或者字母 s.isalpha() #所有字符都是字母 s.isdigit() #所有字符都是数字 s.islower() #所有字符都是小写 s.isupper() #所有字符都是大写 s.istitle() #所有单词都是首字母大写,像标题 s.isspace() #所有字符都是空白字符、\t、\n大小写转换s.upper() #把所有字符中的小写字母转换成大写字母 s.lower() #把所有字符中的大写字母转换成小写字母 s.capitalize() #把第一个字母转化为大写字母...

python转换字符串为摩尔斯电码的方法

本文实例讲述了python转换字符串为摩尔斯电码的方法。分享给大家供大家参考。具体实现方法如下:chars = ",.0123456789?abcdefghijklmnopqrstuvwxyz" codes = """--..-- .-.-.- ----- .---- ..--- ...-- ....- ..... -.... --... ---..----. ..--.. .- -... -.-. -... . ..-. --. .... .. .--- -.- .-.. ---. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..""" keys = dict(zip(chars, codes.split())) def char2morse(char...

python实现文件路径和url相互转换的方法

本文实例讲述了python实现文件路径和url相互转换的方法。分享给大家供大家参考。具体实现方法如下:import urllib pathname = path/to/file/or/folder/ url = urllib.pathname2url(pathname) pathname = urllib.url2pathname(url) print pathname运行结果如下: path\to\file\or\folder\ 希望本文所述对大家的Python程序设计有所帮助。

编写Python脚本把sqlAlchemy对象转换成dict的教程

在用sqlAlchemy写web应用的时候,经常会用json进行通信,跟json最接近的对象就是dict,有时候操作dict也会比操作ORM对象更为方便,毕竟不用管数据库session的状态了。 假设数据库里有一张post表,其中一种方法就是p = session.query(Post).first() p.__dict__但由于p是sqlAlchemy的对象,所以p.__dict__中会有一些其他的属性比如_sa_instance这种我们不需要关注的 那么我们可以给model的基类加一个方法,假设models.py中原来是这样...

Python字符和字符值(ASCII或Unicode码值)转换方法

目的 将一个字符转化为相应的ASCII或Unicode码,或相反的操作。 方法 对于ASCII码(0~255范围) 代码如下: >>> print ord(A) 65 >>> print chr(65) A对于Unicode字符,注意仅接收长度为1的Unicode字符代码如下: >>> print ord(u\u54c8) 21704 >>> print unichr(21704) 哈 >>> print repr(unichr(21704)) u\u54c8chr()和str()区别,一个仅接收0~255的数值返回对应于ASCII值的字符,一个接受任何类型返回字符串格式代码如下: >>> ch...

Python中用于转换字母为小写的lower()方法使用简介

lower()方法返回所有基于大小写的字符被转换为小写字符串的一个副本。 语法 以下是lower()方法的语法:str.lower()参数NA返回值 此方法返回的所有基于大小写字符被转化为小写字符串的一个副本。 例子 下面的例子显示了lower()方法的使用。#!/usr/bin/pythonstr = "THIS IS STRING EXAMPLE....WOW!!!";print str.lower();当我们运行上面的程序,它会产生以下结果:this is string example....wow!!!

Python批量转换文件编码格式

自己写的方法,适用于linux,#!/usr/bin/python #coding=utf-8 import sys import os, os.path import dircache import commands def add(x,y):return x*ydef trans(dirname):lis = dircache.opendir(dirname)for a in lis: af=dirname+os.sep+a ## print afif os.path.isdir(af): ## print af trans(af) else:## print af+"encoding="+fi.nameft = commands.getoutput(file -i +af) ## print ftif a.find(.htm)==-1 and a.find(.x...

python实现将汉字转换成汉语拼音的库

本文实例讲述了python实现将汉字转换成汉语拼音的库。分享给大家供大家参考。具体分析如下: 下面的这个python库可以很容易的将汉字转换成拼音,其中用到了一个word.data 的字典,可点击此处本站下载。#!/usr/bin/env python # -*- coding:utf-8 -*- __version__ = 0.9 __all__ = ["PinYin"] import os.path class PinYin(object):def __init__(self, dict_file=word.data):self.word_dict = {}self.dict_file = dict_filedef load...