【py code diary everyday】2023-02-06

读取文件

10.1.4 创建一个包含文件各行内容的列表

filename = 'pi_digits.txt'
with open(filename) as file_object:
    lines = file_object.readlines()
#方法readlines从文件读取每一行,并将其存储在一个列表中

for line in lines:
    print(line.rstrip())
#上述结论
3.1415926535
  8979323846
  2643383279
[Finished in 47ms]
#打印lines可以看出readline的效果
filename = 'pi_digits.txt'
with open(filename) as file_object:
    lines = file_object.readlines()
#方法readlines从文件读取每一行,并将其存储在一个列表中

print(lines)

for line in lines:
    print(line.rstrip())
['3.1415926535\n', '  8979323846\n', '  2643383279\n']

3.1415926535
  8979323846
  2643383279
[Finished in 47ms]

从第一行能看出来是按照列表的方式展示了文件数据

10.1.5 使用文件的内容

filename = 'pi_digits.txt'

#将所有行都存储在一个列表中
with open(filename) as file_object:
    lines = file_object.readlines()

#创建变量pi_string,使用for循环将各行加入pi_string
#同时删除每行末尾的换行符
pi_string = ''
for line in lines:
    pi_string += line.rstrip()
    
#打印字符串及长度
print(pi_string)
print(len(pi_string))
#结论如下
3.1415926535  8979323846  2643383279
36
[Finished in 62ms]

使用strip来删除每行左边的空格

filename = 'pi_digits.txt'

#将所有行都存储在一个列表中
with open(filename) as file_object:
    lines = file_object.readlines()

#创建变量pi_string,使用for循环将各行加入pi_string
#同时删除每行末尾的换行符
pi_string = ''
for line in lines:
    pi_string += line.strip()
    
#打印字符串及长度
print(pi_string)
print(len(pi_string))
3.141592653589793238462643383279
32
[Finished in 62ms]

课后练习

filename = 'text_files/learning_python.txt'
#打印时读取整个文件
with open(filename) as f:
    contents = f.read()
print(contents.rstrip())
print("-------------")

filename = 'text_files/learning_python.txt'
#打印时遍历文件对象
with open(filename) as file_object:
    for line in file_object:
        print(line.rstrip())
print("-------------")

filename = 'text_files/learning_python.txt'
#打印时将各行存储在一个列表中,在with代码块外打印它们
with open(filename) as file_object:
    lines = file_object.readlines()
for line in lines:
    print(line.rstrip())

结果如下:

In Python you can use the incredibly efficient languanges
In Python you can write "clean" code
In Python you can easy to read the code
In Python you can easy to debug
In Python you can easy to extend compared to
In Python you can make games
In Python you can enter the Python community
-------------
In Python you can use the incredibly efficient languanges
In Python you can write "clean" code
In Python you can easy to read the code
In Python you can easy to debug
In Python you can easy to extend compared to
In Python you can make games
In Python you can enter the Python community
-------------
In Python you can use the incredibly efficient languanges
In Python you can write "clean" code
In Python you can easy to read the code
In Python you can easy to debug
In Python you can easy to extend compared to
In Python you can make games
In Python you can enter the Python community
[Finished in 62ms]

课后练习题:C语言学习笔记

filename = 'text_files/learning_python.txt'
#打印时将各行存储在一个列表中,在with代码块外打印它们
with open(filename) as file_object:
    lines = file_object.readlines()
for line in lines:
    # 依次使用replace和rstrip,称为方法串接
    #使用replace将Python都替换成C
    print(line.replace('Python','C').rstrip())
    #第二套实现方法为先执行rstrip再执行replace
    # line = line.rstrip()
    # print(line.replace('Python','C'))

结果如下:
其中实现方法有两种,一种是方法串接;第二种是先执行“删除行尾的换行符”,再将Python替换成C

In C you can use the incredibly efficient languanges
In C you can write "clean" code
In C you can easy to read the code
In C you can easy to debug
In C you can easy to extend compared to
In C you can make games
In C you can enter the C community
[Finished in 62ms]

用遍历文件的方式也可以实现替换的目的

filename = 'text_files/learning_python.txt'
#打印时遍历文件对象
with open(filename) as file_object:
    for line in file_object:
        print(line.replace('Python','C').rstrip())

结论

In C you can use the incredibly efficient languanges
In C you can write "clean" code
In C you can easy to read the code
In C you can easy to debug
In C you can easy to extend compared to
In C you can make games
In C you can enter the C community
[Finished in 47ms]

同样,使用读取整个文件的方式

filename = 'text_files/learning_python.txt'
#打印时读取整个文件
with open(filename) as f:
    contents = f.read()
print(contents.replace('Python','C').rstrip())
In C you can use the incredibly efficient languanges
In C you can write "clean" code
In C you can easy to read the code
In C you can easy to debug
In C you can easy to extend compared to
In C you can make games
In C you can enter the C community
[Finished in 63ms]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值