这就是我制作CSV文件的方式:
with open('Mail_Txt.csv', 'w', encoding='utf-8', newline='') as csvfile:
fieldnames= ['Sender', 'Subject', 'Snippet']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames , delimiter=',')
for val in final_list:
writer.writerow(val)
我尝试了许多方法来打印CSV文件的"发件人"列。但我总是失败。所以,帮我打印CSV文件的第一列或'Sender'
列
从csv文件读取与写入是对称的。主要区别在于,由于您跳过了标题行,您将使用一个简单的读取器并获取序列,而不是映射:
with open('Mail_Txt.csv', 'r', encoding='utf-8', newline='') as csvfile:
reader= csv.reader(csvfile, delimiter=',')
for val in reader:
print(val[0])
您可以使用DictReader函数来实现这一点。
with open('yourFile') as f:
data = [row["Sender"] for row in DictReader(f)]
print(data)