str1 = 'hello' str2 = 'world' str3 = ' '.join([str1,str2]) print(str3)
一张褪色的照片, 好像带给我一点点怀念。 巷尾老爷爷卖的热汤面, 味道弥漫过旧旧的后院; 流浪猫睡熟在摇晃秋千, 夕阳照了一遍他咪着眼; 那张同桌寄的明信片, 安静的躺在课桌的里面。
f = open('file1','r')
f_read = f.read() #read是逐字符地读取,read可以指定参数,设定需要读取多少字符,无论一个英文字母还是一个汉字都是一个字符。
print(f_read)
f.close()
f = open('file1','r')
f_read = f.readline() #readline只能读取第一行代码,原理是读取到第一个换行符就停止。
print(f_read)
f.close()
f = open('file1','r')
f_read = f.readlines() #readlines会把内容以列表的形式输出。
print(f_read)
f.close()
f = open('file1','r')
for line in f.readlines() #使用for循环可以把内容按字符串输出。
print(line) #输出一行内容输出一个空行,一行内容一行空格... 因为文件中每行内容后面都有一个换行符,而且print()语句本身就可以换行,如果不想输出空行,就需要使用下面的语句:print(line.strip())
f.close()
f = open('file1','w',encoding='utf8') #由于Python3的默认编码方式是Unicode,所以在写入文件的时候需要调用utf8,以utf8的方式保存,这时pycharm(默认编码方式是utf8)才能正确读取,当读取文件时,文件是utf8格式,pycharm也是utf8,就不需要调用了。
f_w = f.write('hello world')
print(f_w) #有意思的是,这里并不打印'hello world',只打印写入多少字符
f.close()
f = open('file1','a')
f_a = f.write('hello world')
print(f_a) #还是会打印写入的字符数
f.close()
f = open('file','r')
data=f.readlines() #注意及时关闭文件
f.close()
num = 0
for i in data:
num += 1
if num == 5:
i = ''.join([i.strip(),'hello world']) #不要使用“+”进行拼接
print(i.strip())
f.close()
num = 0
f.close() #不要过早关闭文件,否则程序不能识别操作句柄f.
f = open('file','r')
for i in f: #for内部把f变为一个迭代器,用一行取一行。
num += 1
if num == 5:
i = ''.join([i.strip(),'hello world'])
print(i.strip())
f.close()
f = open('file','r')
print(f.tell()) #光标默认在起始位置
f.seek(10) #把光标定位到第10个字符之后
print(f.tell()) #输出10
f.close()
----------------------
f = open('file','w')
print(f.tell()) #先清空内容,光标回到0位置
f.seek(10)
print(f.tell())
f.close()
----------------------
f = open('file','a')
print(f.tell()) #光标默认在最后位置
f.write('你好 世界')
print(f.tell()) #光标向后9个字符,仍在最后位置
f.close()
import sys,time #导入sys和time模块
for i in range(40):
sys.stdout.write('*')
sys.stdout.flush() #flush的作用相当于照相,拍一张冲洗一张
time.sleep(0.2)
下面代码也能够实现相同的功能
import time
for i in range(40):
print('*',end='',flush=True) #print中的flush参数
time.sleep(0.2)
f = open('file','a')
f.truncate(6) #只显示6个字节的内容(6个英文字符或三个汉字),后面的内容被清空。
#--------------------------光标总结head-----------------------------------
f = open('file','r')
print(f.read(6)) #6个字符
print(f.tell()) #位置12字节,一个汉字两个字节
f.close()
f = open('file','r')
f.seek(6) #6个字节
print(f.tell())
f.close()
f = open('file','a')
print(f.tell()) #光标默认在最后位置
f.write('你好 世界')
print(f.tell()) #光标向后9个字节,一个汉字两个字节,仍在最后位置 182-->191
f.close()
f = open('file','a',encoding='utf-8')
print(f.truncate(6)) #由于需要光标定位位置,所以也是字节。只显示6个字节的内容(6个英文字母或三个汉字,一个汉字两个字节),后面的内容被清空。
f.close()
#-----------------------------光标总结end---------------------------------
f = open('file','a')
print(f.tell()) #末尾207位置
f.close()
f = open('file','r+')
print(f.tell()) #0位置
print(f.readline()) #读取第一行
f.write('羊小羚') #光标移到末尾207位置并写入
print(f.tell()) #213位置
f.seek(0) #光标移到0位置
print(f.readline()) #读取第一行
f.close()
f2 = open('file2','w',encoding='utf8') #写入的时候必须加utf8
f1 = open('file','r')
num = 0
for line in f1: #迭代器
num += 1
if num == 5:
line = ''.join([line.strip(),'羊小羚\n']) #里面就是对字符串进行操作了
f2.write(line)
f1.close()
f2.close()
num = 0
with open('file','r') as f1,open('file2','w',encoding='utf8') as f2:
for line in f1:
num += 1
if num == 5:
line = ''.join([line.strip(),'羊小羚'])
f2.write(line)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有