简体   繁体   English

Python和列表索引错误

[英]Python and a list index error

i begin learning Python and i have a problem at my first steps. 我开始学习Python,而我的第一步遇到了问题。 I have the following code: 我有以下代码:

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)

Until now all good. 到现在为止一切都很好。 The result is this: 结果是这样的:

[['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'], ['']]

Now i want to get a single column of the two existing columns. 现在,我想获得两个现有列中的一个列。 I write: 我写:

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

This gives me good result but when i try to get the second column so i change the: value = item[1] the result is the following: 这给了我很好的结果,但是当我尝试获取第二列时,我更改了: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  

I think that something is wrong with the first column and especially at the end that it has a " [''] " value. 我认为第一列有问题,尤其是最后有一个[[]]值。 Can someone help me? 有人能帮我吗?

As was told in the comments, you have empty string at the end of the data. 如评论中所告知,数据末尾有空字符串。 You can filter all empty strings out in loading phase like this: 您可以在加载阶段过滤掉所有空字符串,如下所示:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM