class Rec:
cust = []
def readCust(self):
with open('customer.txt', 'r') as csv_file:
reader = csv.reader(csv_file)
for line in reader:
self.cust.append(line)
for i in len(self.cust):
print(self.cust[1])
return self.cust
我试图在追加后获取存储在 cust 列表中索引位置 [1] 的名称,以便当我调用 ciraj 时,它会检查它是否在 cust 列表中可用,如果可用,它将返回 8.5 所以我可以将它与我问用户的数量相乘
8, ciraj, w , 8.5, 700
9, eraj, r , 12.5, 50
10, kraj, w , 10, 450
我不太确定你想做什么,但你不能迭代整数,使用列表来保存“名称”的数据是一个坏主意,但如果你真的想使用列表:
import csv
class Rec:
cust = []
def readCust(self):
with open('test.txt', 'r') as csv_file:
reader = csv.reader(csv_file)
for line in reader:
self.cust.append(line)
return self.cust
def getNum(self, name):
for c in self.cust:
if c[1].strip() == name:
return c[3]
else:
pass
def getCustomer(self, name):
for c in self.cust:
if c[1].strip() ==name:
return c
else:
pass
#note: I used strip() because there are blank spaces before the name
a = Rec()
a.readCust()
print(a.getCustomer('eraj'))
print('number: ',a.getNum('eraj'))
print(a.getCustomer('ciraj'))
print('number: ',a.getNum('ciraj'))
输出:
['9', ' eraj', ' r ', ' 12.5', ' 50']
number: 12.5
['8', ' ciraj', ' w ', ' 8.5', ' 700']
number: 8.5
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句