python学习_22(文件)

1?写一个函数,实现遍历一个数字和字母参杂的字符串,如果碰到字母则替换成,最后隔开的数字作为整体计算求和。
如”ab34aa243dd78eww89”,则替换成的结果为:”342437889”,求和结果为:”791517”

s=?"ab34aa243dd78eww89"
result?=""
count?=0
flag=False
for?i?in?s:
????if?i>=‘a‘?and?i<="z":
????????if?flag=True:
????????????result+=str(count)
????????????flag=False
????????????count=0
????????result+="*"
????else:
????????flag=True
????????count+=int(i)

result+=str(count)

print?(result)
#?encoding?=?UTF-8
import?re
s=?"ab34aa243dd78eww89"
result?=""

s=re.sub(r"[a-z]","*",s)?
arr1=re.split("\\*+",s)?#[‘‘,?‘34‘,?‘243‘,?‘78‘,?‘89‘]
for?i?in?range(len(arr1)):
????count=0
????if?arr1[i].isdigit():
????????for?j?in?arr1[i]:
????????????count+=int(j)
????if?count!=0:
????????arr1[i]?=?str(count)

arr2=re.split("\\d+",s)#[‘**‘,?‘**‘,?‘**‘,?‘***‘,?‘‘]

for?i?in?range(len(arr1)):
????result+=arr1[i]+arr2[i]

print(result)
#?encoding?=?UTF-8
import?re
s=?"ab34aa243dd78eww89"
result?=""

s=re.sub(r"[a-z]","*",s)?
arr1=re.split("\\*+",s)?#[‘‘,?‘34‘,?‘243‘,?‘78‘,?‘89‘]
for?i?in?range(len(arr1)):
????count=0
????if?arr1[i].isdigit():
????????for?j?in?arr1[i]:
????????????count+=int(j)
????if?count!=0:
????????arr1[i]?=?str(count)

arr2=re.split("\\d+",s)#[‘**‘,?‘**‘,?‘**‘,?‘***‘,?‘‘]

for?i?in?range(len(arr1)):
????result+=arr1[i]+arr2[i]

print(result)

2?一个字符串i?am?learning,请依照如下规则转换为数字
abcd–5,?efgh–10,?ijkl–15,?mnop–20,?qrst–25,?uvwx–30?yz–35
转换正确结果为:15?520?151052520152010

rule="abcd–5,efgh–10,ijkl–15,mnop–20,qrst–25,uvwx–30,yz–35"
rule=rule.split(",")
s="i?am?learning"
result=""
for?i?in?s:
????for?r?in?rule:
????????if?i?in?r:
????????????#print?(r.split("–"))
????????????part=r.split("–")[-1]
????????????#print?("part",part)
????????????result?+=part
????????????#print(result)
????????????break
????else:
????????result+=i

print?(result)

3?从控制台输入一串字母,判断是否是连续相同字母,是则输出True,否则输出False。

def?judge_str():
????s=input("请输入一串字符串")
????
????if?s[0]*len(s)==s?and?((s[0]>=‘a‘?and?s[0]<=‘z‘)?or?(s[0]>=‘A‘?and?s[0]<=‘Z‘)):
????????return?True
????else:
????????return?False

print?(judge_str()) 

open()函数
open() 方法用于打开一个文件,返回文件对象

语法格式:
open(file, mode=‘r‘, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
参数说明:
file:文件名称
mode:指定文件的打开方式,其中,‘rt‘为默认方式(t也就是text,代表文本文件)
encoding:编码或者解码方式。默认编码方式依赖平台,如果需要特殊
设置,可以参考codecs模块,获取编码列表。encoding不写的话默认用的是GBK
?newline:换行控制,参数有:None,‘\n‘,‘\r‘,‘\r\n。为None的话,写‘\r’‘\r\n’‘\n’的话全部转换成‘\n’

文件打开方式
1、直接通过open打开

>> fp = open("e:\python\a.txt")
>> print(fp)
<_io.TextIOWrapper name=‘e:\python\a.txt‘ mode=‘r‘ encoding=‘cp936‘>

>> fp = open("e:\python\a.txt")
>> fp.read()

UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xbf in position 2: illegal multibyte sequence

>> fp = open("e:\python\a.txt",encoding="utf8")
>> fp.read()
‘\ufeff((1,2,3),("a","b","c"),("a",1,2))\n(6, \‘abc\‘, False)‘

文件默认打开方式是gbk编码,文件打开的编码需要和文件保存编码一致
如果文件本身以ANSI 保存可以使用默认编码,不指定encoding编码
如果文件以utf-8保存,需要执行encoding为utf8编码,不指定会报错

2、通过with语句打开

>> with open("e:\python\a.txt") as file_obj:
... print(file_obj.read())
...
关闭文件
通过open直接打开的文件需要手动关闭,with方式打开的会自动关闭
>> fp = open("e:\python\a.txt",encoding="utf8")
>> fp.read()
‘\ufeff((1,2,3),("a","b","c"),("a",1,2))\n(6, \‘abc\‘, False)‘
>> fp.close()

读取文件
read([size])
size为读取的长度,以byte为单位。如果不指定参数,表示一次性读取全部内容,以字符串形式返回,并且每一行结尾会有一个"\n"符号;
指定size返回size字节内容

>> with open("e:\python\a.txt") as file_obj:
... print(file_obj.read())
...

>> fp = open("e:\python\a.txt",encoding="utf8")
>> fp.read(1)
‘a‘
>> fp.read(2)
‘bc‘
>> fp.tell()
3

readline([size])
如果不指定size参数,每次读取一行内容,返回字符串,读取后文件指针跳到下一行
如果指定size,size小于一行的字节数,返回该行size字节字符,大于等于一行字节数返回该行内容

>> fp.readline(2)
‘ab‘
>> fp.readline(3)
‘c12‘
>> fp.readline()#只返回剩余字节字符
‘3456789\n‘
>> fp.readline(50)
‘xyz\n‘
>> fp.readline()
‘aaaaaaaa\n‘

readlines([size])
不指定size参数返回文件每行内容组成的列表,列表的每个元素是字符串且结尾有个\n换行符,读取后文件指针在文件末尾
指定参数size:
当时size参数为0 或大于等于文件所有字节数时 返回全部文件内容组成的列表
当size小于一行文件内容时,返回该行内容
当size大于1行大小,小于两行大小时候返回2行
。。。。。

文件内容:
abc
xyz
aaaa

>> fp = open("e:\python\a.txt")
>> fp.readlines(0)
[‘abc\n‘, ‘xyz\n‘, ‘aaaa‘]
>> fp.seek(0,0)
0
>> fp.readlines(2)
[‘abc\n‘]
>> fp.seek(0,0)
0
>> fp.seek(0,0)
0
>> fp.readlines(5)
[‘abc\n‘, ‘xyz\n‘]
>> fp.seek(0,0)
0
>> fp.readlines(8)
[‘abc\n‘, ‘xyz\n‘, ‘aaaa‘]

>> fp = open("e:\python\a.txt")
>> for i in range(10):
... print(fp.readlines(i))
... fp.seek(0,0)
...
[‘abc\n‘, ‘xyz\n‘, ‘aaaa‘]
0
[‘abc\n‘]
0
[‘abc\n‘]
0
[‘abc\n‘]
0
[‘abc\n‘, ‘xyz\n‘]
0
[‘abc\n‘, ‘xyz\n‘]
0
[‘abc\n‘, ‘xyz\n‘]
0
[‘abc\n‘, ‘xyz\n‘]
0
[‘abc\n‘, ‘xyz\n‘, ‘aaaa‘]
0
[‘abc\n‘, ‘xyz\n‘, ‘aaaa‘]
0

写文件
write([str])
write()?方法用于向文件中写入指定字符串。返回写入的字符数,会移动相应的文件指针
在文件关闭前或缓冲区刷新前,字符串内容存储在缓冲区中,这时你在文件中是看不到写入的内容的。
如果文件打开模式带 b,那写入文件内容时,str (参数)要用 encode 方法转为 bytes 形式,否则报错:TypeError: a bytes-like object is required, not ‘str‘。

写入的类型必须是字符串

>> with open("e:\python\aa.txt","w") as file_obj:
... file_obj.write("testwrite")
...
9
>> with open("e:\python\aa.txt","w") as file_obj:
... file_obj.write("中国")
...
2

writelines(seq)
writelines()?方法用于向文件中写入一序列的字符串。可写入列表、元组、字符串,序列中的内容必须是字符串,写入后文件指针在文件末尾

>> content = ["abc\n","xyx\n","123"]
>> with open("e:\python\aa.txt","w",encoding="utf8") as file_obj:
... file_obj.writelines(content)
...

>> content = ("abcd\n","xyx\n","123")
>> with open("e:\python\aa.txt","w",encoding="utf8") as file_obj:
... file_obj.writelines(content)
...
>> tstr = "a\nb\c"
>> with open("e:\python\aa.txt","w",encoding="utf8") as file_obj:
... file_obj.writelines(tstr)
...

file.closed 返回true如果文件已被关闭,否则返回false。
file.mode 返回被打开文件的访问模式。
file.name 返回文件的名称。

>> fp = open("e:\python\a.txt")
>> fp.name
‘e:\python\a.txt‘

>> fp.mode
‘r‘

>> fp.closed
False

文件常用操作方法:
close()
File 对象的 close()方法刷新缓冲区里任何还没写入的信息,并关闭该文件,这之后便不能再进行写入

>> fp = open("e:\python\a.txt")
>> fp.read()
‘abc\nxyz\naaaa‘
>> fp.close()
>> print("文件是否关闭: ",fp.closed)
文件是否关闭: True

flush()
该函数是将缓冲区中的内容写入硬盘。

>> fp = open("e:\python\1008.txt","w+")
>> fp.write("1009")
4
>> fp.flush()
>> fp.close()

fileno()
返回一个整型的文件描述符(file descriptor FD 整型),可用于底层操作系统的 I/O 操作。

>> fp = open("e:\python\a.txt","w+")
>> fp.fileno
<built-in method fileno of _io.TextIOWrapper object at 0x0000000000388B40>
>> fp.fileno()
3

isatty()
检测文件是否连接到一个终端设备,如果是返回 True,否则返回 False。
如果用linux远程工具 xmanage等打开文件

>> fp.isatty()
False

tell()
返回文件的当前位置,即文件指针当前位置

>> fp = open("e:\python\a.txt")
>> fp.read(1)
‘‘
>> fp.close()
>> fp = open("e:\python\a.txt")
>> fp.read(1)
‘a‘
>> fp.tell()
1
>> fp.readline()
‘bc\n‘
>> fp.tell()
5
>> fp.readlines()
[‘xyz\n‘, ‘aaaa‘]
>> fp.tell()
14

seek( offset[, from ] )
seek(offset [,from])这是一个文件定位函数,该方法改变当前文件的位置。Offset变量表示要移动的字节数。From变量指定开始移动字节的参考位置。
如果from被设为0(默认值),这意味着将文件的开头作为移动字节的参考位置。
如果设为1,则使用当前的位置作为参考位置。
如果它被设为2,那么该文件的末尾将作为参考位置。需要注意,如果文件以a或a+的模式打开,每次进行写操作时,文件操作标记会自动返回到文件末尾

模式1、模式2只能用于二进制模式

>> fp = open("e:\python\a.txt")
>> fp.seek(1,1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
io.UnsupportedOperation: can‘t do nonzero cur-relative seeks

>>?import?os
>>?os.linesep
‘\r\n‘
>>?fp?=?open("e:\a.txt","w")
>>?fp.write("a\r\nb\rc\nd")
8
>>?fp.close()
>>?fp?=?open("e:\a.txt","r")
>>?fp.read()
‘a\n\nb\nc\nd‘

a 模式打开 文件指针在最后
读写文件指针都会移动

利用readline()读取所有行

>>?fp?=?open("e:\a.txt","r")
>>?while?1:
...?????content?=?fp.readline()
...?????print?(content)
...?????if?content?=="":
...?????????break
...
hello?world!1

hello?world!2

hello?world!3

hello?world!4

>>?fp.close()

#encding=utf-8

fp?=?open(?"c:\downloads\ip1.txt",‘w‘)
print?("文件是否关闭:",?fp.closed)
print?("文件的访问模式:",?fp.mode)
print?("文件名称:",?fp.name)
#关闭文件
fp.close()

>> fp = open("e:\python\1.txt")
>> fp.read(5)
‘hhqhh‘
>> fp.tell()
5
>> fp.read(1)
‘h‘
>> fp.tell()
6

>> fp.readline(3)
‘hhh‘
>> fp.seek(0,0)
0
>> fp.readline(4)
‘hhqh‘

>> fp.seek(0,0)
0
>> fp.readlines(2)
[‘hhqhhhhhhh\n‘]

习题:一个文件写入
a26
b25
c24
d23
e22
f21
g20
h19
i18
j17
k16
l15
m14
n13
o12
p11
q10
r9
s8
t7
u6
v5
w4
x3
y2
z1

with open("e:\python\0923.txt","w",encoding="utf8") as file_obj:
n = 26
for i in range(97,123):
file_obj.write(chr(i)+str(n)+"\n")
n -= 1
with?open("e:\a.txt",‘w+‘)?as?fp:
????for?i?in?range(26):
????????fp.write(chr(ord("a")+i)+str(26-i)+"\n")
????fp.seek(0,0)
????print?(fp.read())

writelines

flush()

>> fp1 = open("e:\python\2.txt")
>> fp1.fileno()
3

>> fp1.isatty()
False

tell()

练习:
游标的第5个位置,第一次写a,读出来
第二次写b,读出来
读出来第二行的一个游标位置的内容

with open("e:\\python\\2.txt","r+",encoding="utf8") as file_obj:
    file_obj.seek(5,0)
    file_obj.write("a")
    file_obj.seek(5,0)
    print(file_obj.read(1))

    file_obj.seek(5,0)
    file_obj.write("b")
    file_obj.seek(5,0)
    print(file_obj.read(1))

    file_obj.seek(0,0)
    file_obj.readline()

    print(file_obj.read(1))

习题:文件中有两行内容,你在中间再加入一行

with open("e:\\python\\2.txt","r+",encoding="utf8") as file_obj:
    content = file_obj.readlines()
    content.insert(1,"xyyyyyyyy\n")
with open("e:\\python\\2.txt","w",encoding="utf8") as file_obj:
    file_obj.writelines(content)

with open("e:\\python\\2.txt","r+",encoding="utf8") as file_obj:
    content = file_obj.readlines()
    content.insert(1,"xyyyyyyyy\n")
    file_obj.seek(0,0)
file_obj.writelines(content)

fp1 = open("e:\c.txt","w+",encoding="utf8")
fp1.write("123\n")
fp1.write("456\n")

fp1.seek(0,0)
print(fp1.readline())
print(fp1.tell())
fp1.write("abcdfdfd\n")

fp1.seek(0,0)
print(fp1.readlines())
fp1.close()

Seek() 1 ,2只能在二进制模式下使用

>>?fp?=?open("e:\a.txt","rb+")
>>?fp.read()
b‘1234aaxxx\r\ngloryroad\r\nbxxxx\r\n‘
>>?fp.tell()
29
>>?fp.seek(5,1)
34
>>?fp.seek(-5,1)
29
>>?fp.seek(-5,1)
24
>>?fp.read()
b‘xxx\r\n‘
>>?fp.seek(-5,1)
24
>>?fp.seek(2,1)
26
>>?fp.read()
b‘x\r\n‘
>>

>> fp = open("e:\python\1.txt","rb")
>> print(fp.read())
b‘hhqhhhhhhh\r\ns\r\ns\r\ns\r\ns\r\ns\r\ns‘
>> fp.seek(-3,1)
25
>> print(fp.read())
b‘\r\ns‘
>> fp.seek(-3,2)
25
>> print(fp.read())
b‘\r\ns‘
>> fp.seek(-3,2)
25
>> fp.readline()
b‘\r\n‘
>> fp.seek(0,0)
0
>> print(fp.read())
b‘hhqhhhhhhh\r\ns\r\ns\r\ns\r\ns\r\ns\r\ns‘
>> fp.seek(0,0)
0
>> fp.seek(-8,2)
20
>> print(fp.read())
b‘\ns\r\ns\r\ns‘
>>

读取指定行:

>>?count=0
>>?for?line?in?fp:
...?????count+=1
...?????if?count?==2:
...?????????print(line)
...
b‘gloryroad\r\n‘

print(fp.readlines()[1])

截取文件为指定字节大小

>> fp = open("e:\python\2.txt","r+")
>> fp.truncate(5)
5

#清理缓存,如果你不再需要先前从getline()中得到的行
linecache.clearcache()

判断空行
line.strip() == “”

fp=open("e:\\a.txt","r+")
lines=?fp.readlines()
fp.seek(0,0)
for?line?in?lines:
????if?not?line.strip()=="":
????????fp.write(line)

fp.close() 

>> exec("a=100")
>> a
100

原文地址:http://blog.51cto.com/13496943/2310334

时间: 2024-10-18 19:24:36

python学习_22(文件)的相关文章

python学习之-文件和对象

文件和对象文件对象不仅可以用来访问普通的磁盘文件,还能够来访问普通的磁盘文件,也可以访问任何其他类型抽象层面的"文件".内建函数 open() 返回一个文件对象,对该文件进行后续相关的操作都要用到它.文件内建函数 open() 以及 file() 提供了初始化输入/输出(I/O)操作的通用接口.open() 内建函数成功打开文件后会返回一个文件对象,否则引发一个错误.当操作失败,会产生一个IOERROR异常.内建函数 open() 的基本语法:file_object = open(fi

Python学习_06_文件、IO

文件对象 python中的文件操作和c语言比较类似,包括一些缓冲.偏移量的方式. 文件对象可以通过open().file()两个内建方法创建,两个方法并没有什么不同,使用方法和c语言中的fopen()类似: file_object = open(file_name, access_mode='r', buffering=-1) 其中access_mode表示打开方式,r表示只读,w表示只写,r+,w+表示读写,a表示追加等:buffering表示缓冲方式,负值表示默认缓冲方式,0表示不缓冲,1表

Python学习之--文件操作

Python中对文件操作可以用内置的open()函数 读文件 f=open('/home/test/test.txt','r') # 读模式打开文件 f.read() # 读取文件内容 除了正常的读取文件内容,一个常用的操作是判断文件内容是否为空,如下: if len(f.read())==0: # 如果文件内容为空 xxxx 判断一个文件或者路径是否存在,如下(这里需要在代码中导入os module: import os): file='/home/test/test.txt' dir='/h

PYTHON学习之文件操作;

文件内容替换for line in flielinput.input("filepath",inplace=1):line = line.repace("oldtext","newtext")print line, python中对文件.文件夹(文件操作函数)的操作需要涉及到OS模块和shutil模块.1.得到当前工作目录,即当前PYTHON脚本工作的目录路径:os.getcwd()2.返回指定目录下的所有文件和目录名:os.listdir()3

python学习——大文件分割与合并

在平常的生活中,我们会遇到下面这样的情况: 你下载了一个比较大型的游戏(假设有10G),现在想跟你的同学一起玩,你需要把这个游戏拷贝给他. 然后现在有一个问题是文件太大(我们不考虑你有移动硬盘什么的情况),假设现在只有一个2G或4G的优盘,该怎么办呢? 有很多方法,例如winrar压缩的时候分成很多小卷,这里不累述. 在学习python之后,我们自己就可以解决这个问题啦. 我们可以自己写一个脚本去分割合并文件,将文件分割成适合优盘大小的小文件,在拷贝,然后再合并. 下面是文件分割脚本: 1 im

python学习8 文件的操作

本文拷贝了on testing 的<python之文件操作:文件的读写>,只做学习之用 python的文件读写通过 一.用open函数 二.对文件读写操作 三.读取文件位置定位 1. open函数open(file_name[,access_mode][,buffering]) (1)参数说明 open(file_name[,access_mode][,buffering])      打开文件,获得文件的句柄 file_name:包含文件路径的字符串,可以是绝对路径也可以是相对路径,尽量使用

Python学习:文件

内置open()函数会创建一个Python文件对象,可以作为计算机上的一个文件链接.在调用open()之后,可以通过调用返回文件对象的方法来读写相关外部文件. 打开文件: output = open('C:\spam', 'w')       #打开文件,模式为写入 input = open('C:\spam', 'r')        #打开文件,模式为读出 input = open('C:\spam')             #同上,默认为'r' 文件的读操作: aString = f.r

python学习11 -文件,流

打开文件 语法如下:open(name,[module[,buffering]]) ,模式 和缓冲参数都是可选的 f = open(r'C:\TEXT\somefile.txt')#如果文件不存在 Traceback (most recent call last): File "<pyshell#64>", line 1, in <module> f = open(r'C:\TEXT\somefile.txt')IOError: [Errno 2] No suc

python学习笔记文件操作(六)

1.文件操作流程: 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 如下文件: 2017-03-24 11:25:06:349 - info: [debug] [AndroidBootstrap] Sending command to android: {"cmd":"shutdown"} 2017-03-24 11:25:06:355 - info: [debug] [AndroidBootstrap] Received command