Python读取文件的三种方式


5个月前 260次点击 来自 其他

标签: Python

一次性读取内容

with open('some.txt', 'r', encoding='utf-8') as f:
   print(f.read())

for-in 循环读取内容

with open('some.txt', 'r', encoding='utf-8') as f:
   for line in f:
	    print(line, end='')

按行读取为列表形式

with open('some.txt', 'r', encoding='utf-8') as f:
   lines = f.readlines()

Made with in Shangrao,China By Devler.

Copyright © Devler 2012 - 2022

赣ICP备19009883号-1

Top ↑