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=Falsecount=0result+="*"else:flag=Truecount+=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=0if 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=0if 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)breakelse:result+=iprint (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 Trueelse:return Falseprint (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

python学习_22(文件)相关推荐

  1. [Python学习日记] 文件与文件系统(一)

    [Python学习日记] 文件与文件系统(一) 目录 Python3 File(文件) 方法 open() 方法 file 对象 目录 Python3 File(文件) 方法 open() 方法 Py ...

  2. python学习之文件处理

    Table of Contents 图片成批resize 多个文件夹中某文件,都保存到另一文件夹中 多个文件夹中某文件,都保存到另一文件夹中,并重命名 多个文件夹中,1/4数据用做测试集,其它用作训练 ...

  3. Python学习笔记——文件写入和读取

    1.文件写入 #coding:utf-8 #!/usr/bin/env python 'makeTextPyhton.py -- create text file'import os ls = os. ...

  4. Python学习:文件操作

    一.打开文件 1.格式 在python,使⽤open函数,可以打开⼀个已经存在的⽂件,或者创建⼀个新⽂件,语法如下: open(name, mode) name:是要打开的⽬标⽂件名的字符串(可以包含 ...

  5. python学习之文件读写

    实现文件的读写 #! /usr/bin/python file_add = open('test.txt','a') for i in range(1,5): file_add.write(" ...

  6. Python学习之==文件操作

    1.打开文件的模式 1 r,只读模式(默认)[不可写:文件不存在,会报错] 2 w,只写模式[不可读:不存在则创建:存在则删除内容] 3 a,追加模式[不可读:不存在则创建:存在则追加内容] 4 r+ ...

  7. Python学习12 文件的读写

    目标 文件的打开和关闭 mode缺省情况下表示只读r 文件的路径 前面加个r',代表其中的转义字符不起作用 文件的打开方式 案例:实现文件拷贝的功能 将某一文件夹下的某一文件 保存在当前文件 所在目录 ...

  8. Python 学习笔记——文件对象和操作

    Python的文件处理和相关输入输出能力.介绍文件对象(它的内建函数,内建方法和属性),标准文件,同时讨论文件系统的访问方法,文件执行,以及相关文件模块. 一,内建函数open()和file() 语法 ...

  9. Python学习之文件13

    文件操作 文件操作的流程 1. 建立文件对象: 2. 文件操作(读或者写) 3. 关闭文件 文件读操作 f = open('小重山','r',encoding = 'utf8') '''获得文件对象, ...

最新文章

  1. 自动填充脚本使用及注意事项
  2. JS 事件冒泡和事件捕获
  3. 都2021年了,不会还有人连深度学习都不了解吧(一)- 激活函数篇
  4. TabHost两种实现方式
  5. 转: 微博的多机房部署的实践(from infoq)
  6. 计算机网络 实验教案,《计算机网络》实验教案.pdf
  7. 利用pandas对一列/多列进行数据区间筛选
  8. Nodejs 文件 与 路径 相关用法实例解析
  9. 工程管理 -- makefile
  10. php 怎么写配置文件,在PHP中如何把数组写成配置文件
  11. 解决ppt复制到Word的图片导出PDF后出现黑线问题,word转PDF图片不清晰的问题,ppt转矢量图问题
  12. catia二次开发招标_CATIA二次开发
  13. 克鲁伊夫:巴萨孤独求败 无巨星照样演完美风暴(2009-11-26)
  14. 防火墙多选路出口(ISP选路、策略路由、智能选路)
  15. 通常环境光照度参照表
  16. 图片文件太大?缩小图片文件的2个小技巧
  17. Android 重读官方文档 1
  18. Quectel EC20 获取 MCC,MNC,APN
  19. Python最优化算法学习笔记(Gurobi)
  20. 特征工程之数据预处理与可视化

热门文章

  1. IE6页面最小宽度的设置方法
  2. php控件不显示,解决控件遮挡问题:关于有窗口元素和无窗口元素
  3. vc++6.0获取磁盘基本信息_分享一个实用脚本--一键获取linux内存、cpu、磁盘IO等信息...
  4. SqlServer中获取所有数据库,所有表,所有字段
  5. JavaEE XML的读写(利用JDom对XML文件进行读写)
  6. yd的拔钉子之路之 POI 2017
  7. IIS Express总结
  8. Excel,此文件中的某些文本格式可能已经更改,因为它已经超出最多允许的字体数。...
  9. 百度机器人对战人类最强大脑,赢在了小数点后第二位
  10. 数字图像处理目录列表