我得到了一个带有订单清单的csv文件,看起来像这样:
CUSTOMER_CODE,CUSTOMER_NAME,NAME,PRODUCT
1044, C1, Name1, Arduino,
1044, C1, Name1, ESP8266,
1048, C2, Name1, Arduino Uno,
1042, C3, Name1, ESP32,
1049, C4, Name1, Arduino Mega,
1042, C3, Name1, Nexus 4,
现在我只想提取客户代码列表
[1042, 1044 ,1048 ,1049]
不
[1042, 1044 ,1044,1044,1044,1044,1044,1044,1048,1048,1048,1048,1048,1048,1048,1049 etc.]
#!/usr/bin/python
import MySQLdb, csv
CUSTOMER_CODES = []
with open('Customers.csv','r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if len(CUSTOMER_CODES) == 0:
#adding 1st value
CUSTOMER_CODES.append(int(row['CUSTOMER_CODE']))
for i in range(0,len(CUSTOMER_CODES)):
#check each value of table
print CUSTOMER_CODES
if CUSTOMER_CODES[i] == int(row['CUSTOMER_CODE']):
print "Code is already here "+ str(row['CUSTOMER_CODE'])
else:
CUSTOMER_CODES.append(int(row['CUSTOMER_CODE']))
而不是像这样的输出:
[1044, 1045, 1047....]
我有这个:
[1044, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1045, 1047, 1047, 1047, 1047, 1047, 1047, 1047, 1047, 1047,
最佳答案
只需使用set
代替list
:
#!/usr/bin/python
import MySQLdb, csv
CUSTOMER_CODES = set()
with open('Customers.csv','r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
CUSTOMER_CODES.add(int(row['CUSTOMER_CODE']))
或使用集合理解(假设Python 2.6+):
#!/usr/bin/python
import MySQLdb, csv
with open('Customers.csv','r') as csvfile:
reader = csv.DictReader(csvfile)
CUSTOMER_CODES = {int(row['CUSTOMER_CODE']) for row in reader}
如果要排序列表,请添加
CUSTOMER_CODES = sorted(CUSTOMER_CODES)
。