设为首页 收藏本站
查看: 838|回复: 0

[经验分享] Python 学习系列(四)

[复制链接]

尚未签到

发表于 2017-4-25 12:05:40 | 显示全部楼层 |阅读模式
这篇文章继续介绍Python的IO内建函数,下面是我做的一个Sample:
'''
Created on 2010-4-4
@author: Jamson Huang
'''
#解析字符串函数以及IO函数
import sys
#import os
import math
if __name__ == '__main__':
#gloabal zone   
a = 5
count = 0
tempStr = ['homework','something', 'python','jamson']
tempZip = ['Children', 'do', 'Male', 'user']
myDicts = {('China','Hubei'):('yourName','Jamson')}
tempTable = {'china':86,'singpore':65}
globalStr = 'python is loved language!'
filePath = 'C:/python/mylog.txt'
#string function
#注意:当全局变量和局部变量相同时,会出现error:  UnboundLocalError  
def strFunc():
print('repr():', repr(globalStr))
print('repr():', repr(0.1))
print(globalStr.zfill(50))
print('we are the {0} who says {1}'.format('chinese', 'chinese'))
print('The value of PI is approximately {0:.4f}'.format(math.pi))
for countryStr,zipStr in tempTable.items():
print('{0:10}=>{1:10d}'.format(countryStr, zipStr))
tempTable1 = {'china':'jamson', 'singapore':'Liew Sig'}
for countryStr,personStr in tempTable1.items():
print('{0:10}=>{1:s}'.format(countryStr, personStr))
myFile = open(filePath,'r')
for line in myFile:
print(line, end='')
strFunc()
#readline 和readlines的不同
#tell():方法返回一个指代文件对象当前位置的整数,表示从文件开头到当前位置的字节数。   
#seek(offset, from_what):  新的位置是通过将 offset 值与参考点相加计算得来的,
#    参考点是由 from_what 参数确定的。 如果 from_what 值为0则代表从文件头开始计算,
#    值为1时代表从当前文件位置开始计算,值为2时代表从文件尾开始计算。 from_what 参数
#    可以省略并且其默认值为0,即使用文件头作为参考点      
def fileIoFunc():
myFile = open(filePath,'rb+')
print(myFile.readline())#注意大小写
print(myFile.tell())
print(myFile.seek(10))
print(myFile.tell())
print(myFile.seek(11,1))
print(myFile.tell())
#        print(myFile.size())
#        print(myFile.readlines())
myFile.close()
#        myFile.read()
try:
myFile.read()
except ValueError:
print(sys.stderr, 'IO Read Error')
finally:
print('IO read close')
#Traceback (most recent call last):
#  File "<stdin>", line 1, in ?
#ValueError: I/O operation on closed file        
fileIoFunc()
#with key in file IO:这个方法在很多高级语言中都使用(如java)
#open(filename, mode)  
#    通常,文件是以 text mode (文本模式)方式打开,
#    即你从文件中读写字符串都是以一种特殊编码(默认为UTF-8)进行编码的。 可以通过在常用模式后添加 'b'
#     选项从而以 binary mode (二进制模式)打开文件,现在数据就是以字节码对象形式来读写了。
#     这种模式可以用在所有非文本文件中,例如:jpeg图片。      
def withFunc():
with open(filePath, 'r') as f:
print(f.read())
f.close()
withFunc()
#pickle():pickling and unpickling
#    它几乎可以将任何Python对象(甚至是一些Python代码!)转换为字符串表示形式,这个过程称为
#    pickling (封装)。 从这个字符串表示形式中重建Python对象被称为 unpickling (拆封)。
#    在 pickling 和 unpickling 之间,字符串表示的对象可以存储在文件或数据中,
#    或者可以通过网络连接发送给远程的机器。被称为persistent 对象(持久化对象)
import pickle
def pickleFunc():
x = ''
with open(filePath, 'r') as f:
x = pickle.load(f)
print(x)
#            pickle.dump(x, f)
Run Python, Console输出结果为:
repr(): 'python is loved language!'
repr(): 0.1
0000000000000000000000000python is loved language!
we are the chinese who says chinese
The value of PI is approximately 3.1416
singpore  =>        65
china     =>        86
china     =>jamson
singapore =>Liew Sig
If you do much work on computers, eventually you find that there’
s some task you’d like to automate. For example, you may wish.b'If you do much work on computers, eventually you find that there\xa1\xaf\r\n'
68
10
10
21
21
<_io.TextIOWrapper name='<stderr>' encoding='UTF-8'> IO Read Error
IO read close
If you do much work on computers, eventually you find that there’
s some task you’d like to automate. For example, you may wish.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-369111-1-1.html 上篇帖子: python 搜索 PDF文件 内容 下篇帖子: 开发笔记:Python中的Module
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表