如何在 python 中打印文本文件的 select 行?
How to print select lines of a text file in python?
我有一个包含多行文本的文本文件。每行文本分为两列,用逗号分隔。如何编写程序来仅打印文本文件中第一列具有特定值的行?因此,例如,我如何编写一个程序来打印第一列为 "hello" 的每一行?
我正在使用 python 3.3.3
#!/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='')
我有一个包含多行文本的文本文件。每行文本分为两列,用逗号分隔。如何编写程序来仅打印文本文件中第一列具有特定值的行?因此,例如,我如何编写一个程序来打印第一列为 "hello" 的每一行?
我正在使用 python 3.3.3
#!/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='')