我有一个包含多行文本的文本文件。每一行文本被分割成两列,用逗号分隔。如何编写程序,仅打印第一列具有特定值的文本文件行?例如,我如何编写一个程序来打印以"hello“作为第一列的每一行?
我正在使用python 3.3.3
发布于 2015-03-04 19:22:03
#!/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='')https://stackoverflow.com/questions/28862918
复制相似问题