首页 > IT职业 > IT生活 > python如何迭代文件中的内容

python迭代文件中的内容的内容的方法:
使用open函数打开文件,用readlines函数获取文件的内容,然后用for循环文件内容就可以了
示例代码如下:
file = open('vv.txt', 'r')
source = file.readlines()
for i in source:
print(i.strip('n'))
执行结果:

更多Python知识,请关注:Python自学网!!
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2063/viewspace-2833298/ ,如需转载,请注明出处,否则将追究法律责任。