且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用行号python打印出每一行(从文件中)

更新时间:2023-02-05 09:29:01

您应该使用枚举器和迭代器,而不是将整个文件读取到内存中.

You should use enumerators and iterators rather than reading the entire file to memory:

with open('something.txt', 'r') as f:
    for i, line in enumerate(f, start=1):
        print('{} = {}'.format(i, line.strip()))