我有一个简单的函数来读取 csv 文件并从中提取第一个列:
import csv
def pass_username():
with open('test.csv', 'r') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
return row[0]
当我将此函数称为:
a = pass_username()
print a
这仅打印第一个元素。但是,当我用as替换return
word并调用该函数时,它会打印所有元素。我想将该函数分配给一个变量,因此我想使用 return。如何解决?print
print row[0]
pass_username()
test.csv 的内容:
"test@gmail.com","rockon"
"hello@gmail.com","hey"
"hithere@gmail.com","ok"
"hellosir@gmail.com","password"