如何输出 test.txt 文件的第 4 行到第 14 行

本文介绍了两种方法来输出 test.txt 文件中的第 4 行到第 14 行内容,分别是使用 Python 和 Linux 命令行。

方法一:使用 Python 编程语言

with open('test.txt', 'r') as file:
    lines = file.readlines()[3:14]  # 获取第 4 行到第 14 行的内容
    for line in lines:
        print(line.strip())  # 输出每一行的内容

方法二:使用 Linux 命令行

sed -n '4,14p' test.txt

请确保你已经在正确的目录下,并且 test.txt 文件存在。

如何输出 test.txt 文件的第 4 行到第 14 行

原文地址: https://www.cveoy.top/t/topic/o1rn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录