我有一个包含两列的CSV文件,我想打印第二列的内容。
例如:(来自此)
for row in file1:
print(row[1])
到此:
print([row[1] for row in file1])
问题是它没有打印任何东西。谁能告诉我哪里错了?
答案 0 :(得分:0)
不使用任何库的解决方案
with open('file.txt') as file1:
print([x.split(",")[1] for x in file1.read().split("\n")])
更优雅的输出
with open('file.txt') as file1:
print("\n".join([x.split(",")[1] for x in file1.read().split("\n")]))
答案 1 :(得分:0)
尝试:
[print(row[1]) for row in file1]
Edit:
要避免打印第一行(主要是标题),请执行
next(file1)
[print(row[1]) for row in file1]