Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
加载文件内容后,如何让 Python3 打印文件的第一行?换句话说,Python3 与 AWK 的等价物是awk "NR==1"什么?
awk "NR==1"
要打印文件的第一行,试试这个:
with open('my_file_name') as in_file: print(next(in_file))
文件对象是可迭代的行。所以在这样的for循环中:
with open('my_file_name') as in_file: for line in in_file: print line
将打印文件的所有行。