如何在python中打印文本文件的选择行?

时间:2015-03-04 19:15:38

标签: python-3.x printing text-files line

我有一个包含多行文字的文字文件。每行文本分为两列,用逗号分隔。如何编写程序以仅打印具有第一列特定值的文本文件的行?那么例如我如何编写一个程序来打印每一行都有"你好"作为第一列?

我正在使用python 3.3.3

1 个答案:

答案 0 :(得分:0)

#!/usr/bin/env python3
import sys

filename = sys.argv[1]

# read the file line by line
with open(filename) as f:
    for line in f:
        # split the line
        columns = line.split(",")

        # print all lines with "hello" as the first column
        if columns[0] == "hello":
            print(line, end='')