【发布时间】:2017-07-31 18:51:12
【问题描述】:
我有一个如下所示的 csv 文件:
name1, id1, email1, uID1
name2, id2, email2, uID2
name3, id3, email3, uID3
name4, id4, email4, uID4
name5, id5, email5, uID5
name6, id6, email6, uID6
我想从中获取一封随机电子邮件。例如。我想要email4,只有email4。我怎么读进去?我不想要 name4 id4 和 uID4,只需 email4。
注意:我正在编写一种方法来执行此操作,并且希望返回 email4 而不是打印它。
我已经看到很多关于如何获取整行或整列的信息,但没有看到如何获取一行的一部分。我该怎么做?
我已经查看并尝试了此线程上的所有选项:How can I get a specific field of a csv file? 但答案对我不起作用。所以新的解决方案或解决方案的修复会很棒!
这是我现在所在的位置:
num = random.randint(1,11)
with open('Accounts_details.csv', 'rb') as f:
reader = csv.reader(f)
reader = list(reader)
text = reader[num][2]
print(text)
这会引发错误:
reader = list(reader)
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
【问题讨论】: