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

转载于:https://blog.51cto.com/13496943/2310334

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值