首先我想說我是不是要求你寫代碼。我只想討論並獲得關於編寫此程序的最佳方法的反饋,因爲我一直在研究如何解決問題。將CSV文件讀入字典?
我的程序應該打開它包含7列的CSV文件:
Name of the state,Crop,Crop title,Variety,Year,Unit,Value.
下面是該文件的一部分:
Indiana,Corn,Genetically engineered (GE) corn,Stacked gene varieties,2012,Percent of all corn planted,60
Indiana,Corn,Genetically engineered (GE) corn,Stacked gene varieties,2013,Percent of all corn planted,73
Indiana,Corn,Genetically engineered (GE) corn,Stacked gene varieties,2014,Percent of all corn planted,78
Indiana,Corn,Genetically engineered (GE) corn,Stacked gene varieties,2015,Percent of all corn planted,76
Indiana,Corn,Genetically engineered (GE) corn,Stacked gene varieties,2016,Percent of all corn planted,75
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2000,Percent of all corn planted,11
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2001,Percent of all corn planted,12
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2002,Percent of all corn planted,13
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2003,Percent of all corn planted,16
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2004,Percent of all corn planted,21
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2005,Percent of all corn planted,26
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2006,Percent of all corn planted,40
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2007,Percent of all corn planted,59
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2008,Percent of all corn planted,78
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2009,Percent of all corn planted,79
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2010,Percent of all corn planted,83
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2011,Percent of all corn planted,85
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2012,Percent of all corn planted,84
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2013,Percent of all corn planted,85
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2014,Percent of all corn planted,88
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2015,Percent of all corn planted,88
Indiana,Corn,Genetically engineered (GE) corn,All GE varieties,2016,Percent of all corn planted,86
然後閱讀每一行到字典中。在這個文本文件中有許多行,我想要/需要的唯一行是Variety列的內容爲「所有GE品種」的行。請注意每個州也有多條線路。下一步是使用作物的用戶輸入並僅檢查該作物的數據。最後一步是找出(每個州)什麼是最大值和最小值及其相應的年份並打印出來。
我想這樣做的方式可能是爲每一行創建一個集合,檢查「所有GE變種」是否在集合中,並且如果它是將它添加到字典中。然後做一些類似的作物?
我最大的困惑可能是1)我不知道如何去忽略不包含「所有GE品種」的品種。在創建字典之前或之後,我會這樣做嗎?和2.)我知道如何創建一個具有一個值和一個關鍵字的字典,但我怎樣才能將其餘的值添加到關鍵字?你有套嗎?或列表?
什麼是他們的關鍵,什麼是價值? –
您可以使用標準庫中的'csv'模塊。 –
@DmitryPolonskiy該鍵應該是州名,該值應該是作物名稱,品種,年份和價值。 –