Python和列表索引错误

时间:2018-07-19 13:52:14

标签: python

我开始学习Python,而我的第一步遇到了问题。 我有以下代码:

f=open("C:\$%$%$%$.csv","r")
data=f.read()
rows = data.split('\n')
final_data = []
for row in rows:
    split_list = row.split(',')
    final_data.append(split_list)

直到现在一切都很好。结果是这样的:

[['product_name', 'prod_orders'], ['Banana', '472565'], ['Bag of Organic Bananas', '379450'], ['Organic Strawberries', '264683'], ['Organic Baby Spinach', '241921'], ['Organic Hass Avocado', '213584'], ['Organic Avocado', '176815'], ['Large Lemon', '152657'], ['Strawberries', '142951'], ['Limes', '140627'], ['Organic Whole Milk', '137905'], ['Organic Raspberries', '137057'], ['Organic Yellow Onion', '113426'], ['Organic Garlic', '109778'], ['Organic Zucchini', '104823'], ['Organic Blueberries', '100060'], ['Cucumber Kirby', '97315'], ['Organic Fuji Apple', '89632'], ['Organic Lemon', '87746'], ['Apple Honeycrisp Organic', '85020'], ['Organic Grape Tomatoes', '84255'], ['Seedless Red Grapes', '82689'], ['Organic Cucumber', '80392'], ['Honeycrisp Apple', '79769'], ['Organic Baby Carrots', '76896'], ['Organic Half & Half', '76360'], ['Sparkling Water Grapefruit', '75886'], ['Organic Large Extra Fancy Fuji Apple', '75165'], ['Yellow Onions', '73167'], ['Organic Gala Apples', '72846'], ['Organic Baby Arugula', '72829'], ['Carrots', '72736'], ['Fresh Cauliflower', '71584'], ['Original Hummus', '71314'], ['Organic Cilantro', '69524'], ['Half & Half', '69217'], ['Organic Small Bunch Celery', '68091'], ['Michigan Organic Kale', '67449'], ['Asparagus', '67283'], ['Organic Red Onion', '66986'], ['Organic Tomato Cluster', '64289'], ['Organic Blackberries', '61746'], ['100% Whole Wheat Bread', '60816'], ['Organic Italian Parsley Bunch', '60621'], ['Organic Whole String Cheese', '59676'], ['Organic Granny Smith Apple', '58779'], ['Organic Unsweetened Almond Milk', '57895'], ['Raspberries', '57640'], ['Organic Red Bell Pepper', '57485'], ['Spring Water', '56087'], ['Blueberries', '55946'], ['Green Bell Pepper', '55484'], ['Organic Peeled Whole Baby Carrots', '55371'], ['Red Peppers', '55280'], ['Red Vine Tomato', '54409'], ['Organic Ginger Root', '52087'], ['Organic Garnet Sweet Potato (Yam)', '51939'], ['Boneless Skinless Chicken Breasts', '50281'], ['Extra Virgin Olive Oil', '50255'], ['Hass Avocados', '50221'], ['Organic Kiwi', '50141'], ['Unsweetened Almondmilk', '49569'], ['Small Hass Avocado', '49021'], ["Organic D'Anjou Pears", '48915'], ['Organic Reduced Fat 2% Milk', '47839'], ['Lime Sparkling Water', '46546'], ['Bunched Cilantro', '45530'], ['Grated Parmesan', '45238'], ['Organic Lacinato (Dinosaur) Kale', '43067'], ['Red Onion', '43009'], ['Sparkling Natural Mineral Water', '42934'], ['Organic Navel Orange', '42721'], ['Jalapeno Peppers', '42561'], ['Broccoli Crown', '42044'], ['Uncured Genoa Salami', '41473'], ['Large Alfresco Eggs', '40376'], ['Roma Tomato', '40212'], ['Organic Grade A Free Range Large Brown Eggs', '40045'], ['Orange Bell Pepper', '39188'], ['Grape White/Green Seedless', '38971'], ['Organic Broccoli', '38720'], ['Organic Black Beans', '38001'], ['100% Raw Coconut Water', '37973'], ['Organic Bartlett Pear', '37706'], ['2% Reduced Fat Milk', '37091'], ['Garlic', '36617'], ['Shredded Parmesan', '36365'], ['Granny Smith Apples', '36187'], ['Organic Carrot Bunch', '35820'], ['Soda', '35791'], ['Unsalted Butter', '35667'], ['Organic Reduced Fat Milk', '35663'], ['Whole Milk', '35633'], ['Bartlett Pears', '35413'], ['Organic Romaine Lettuce', '34874'], ['Unsweetened Original Almond Breeze Almond Milk', '34583'], ['Organic Sticks Low Moisture Part Skim Mozzarella String Cheese', '34209'], ['Pure Irish Butter', '33908'], ['Organic Broccoli Florets', '32887'], ['Sparkling Lemon Water', '32788'], ['']]

现在,我想获得两个现有列中的一个列。我写道:

products = []
for item in final_data:
 value = item[0]
 products.append(value)
print(products)   

这给了我很好的结果,但是当我尝试获取第二列时,我更改了:value = item [1],结果如下:

IndexError                                Traceback (most recent call last)
<ipython-input-81-38d42e19b962> in <module>()
  1 products = []
  2 for item in final_data:
  3     value = item[1]
  4     products.append(value)
  5 print(products)
 IndexError: list index out of range  

我认为第一列有问题,尤其是结尾处有一个[[]]值。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

正如评论中所告知的,您在数据末尾有一个空字符串。您可以在加载阶段过滤掉所有空字符串,如下所示:

f=open("C:\$%$%$%$.csv","r")
data=f.read()
rows = data.split('\n')
final_data = []
for row in rows:
    split_list = row.split(',')
    if len(split_list) == 1:
        continue
    final_data.append(split_list)