2016-11-27 11 views
0

我已經使用DictReader函數將csv文件加載到我的程序中。我的任務是將csv文件的每一列附加到標有「文件名」的單獨列表中加上計數。一旦轉換成字典,我使用這段代碼,但它只附加第一個臨時列表到我的最後一個字典。有人可以在這裏指出這個問題嗎?在CSV到字典和列表轉換解析器中的未知錯誤

import csv 
with open(cities_new, 'r') as g: 
    files = csv.DictReader(g) 
    filenames = ['name', "timeZone_label", "utcOffset", "homepage", 
    "governmentType_label", 
    "isPartOf_label", "areaCode", "populationTotal", "elevation", 
    "maximumElevation", "minimumElevation", "populationDensity", 
    "wgs84_pos#lat", "wgs84_pos#long", "areaLand", "areaMetro", 
    "areaUrban"] 

    dict_1 = {} 
    count_2 = 0 
    for name in filenams: 
     lst = [] 
     for row in files: 
      lst.append(row[name]) 
     count_2+=1 
     dict_1['filename'+str(count_2)] = lst 

這裏是我的輸出:

{'filename1': ['Indian Standard Time', 
    'Indian Standard Time', 
    'Indian Standard Time', 
    'Indian Standard Time', 
    'Indian Standard Time', 
    'Indian Standard Time', 
    'Central Time Zone (North America)', 
    'Central Time Zone (North America)', 
    'Central Time Zone (North America)', 
    'Central Time Zone (North America)', 
    'Central Time Zone (North America)', 
    'Alaska Time Zone', 
    'Alaska Time Zone', 
    'Pacific Time Zone', 
    'Alaska Time Zone', 
    'Alaska Time Zone', 
    'Alaska Time Zone', 
    'Alaska Time Zone', 
    'Alaska Time Zone', 
    'Pacific Time Zone', 
    'Pacific Time Zone', 
    'Pacific Time Zone', 
    'Pacific Time Zone', 
    'Pacific Time Zone', 
    'Eastern Time Zone', 
    'Central Time Zone (North America)', 
    'Central Time Zone (North America)', 
    'Central Time Zone (North America)', 
    'Central Time Zone (North America)', 
    'Central Time Zone (North America)', 
    'Central Time Zone (North America)', 
    'Central Time Zone (North America)', 
    'Central Time Zone (North America)', 
    'Central Time Zone (North America)', 
    'Pacific Time Zone', 
    'Time in China', 
    'NULL', 
    'Central Time Zone (North America)', 
    'Central Time Zone (North America)'], 
'filename10': [], 
'filename11': [], 
'filename12': [], 
'filename13': [], 
'filename14': [], 
'filename15': [], 
'filename16': [], 
'filename2': [], 
'filename3': [], 
'filename4': [], 
'filename5': [], 
'filename6': [], 
'filename7': [], 
'filename8': [], 
'filename9': []} 

任何幫助,將不勝感激感謝。 這裏是逐行csv文件的純文本副本。我把它縮減爲五行,但它仍然很長。

"URI","rdf-schema#label","rdf-schema#comment","administrativeDistrict_label","administrativeDistrict","anthem_label","anthem","area","areaCode","areaLand","areaMetro","areaRural","areaTotal","areaUrban","areaWater","city_label","city","code","country_label","country","daylightSavingTimeZone_label","daylightSavingTimeZone","district_label","district","division_label","division","elevation","federalState_label","federalState","foundingDate","foundingPerson_label","foundingPerson","foundingYear","governingBody_label","governingBody","government_label","government","governmentType_label","governmentType","isPartOf_label","isPartOf","isoCodeRegion_label","isoCodeRegion","leader_label","leader","leaderName_label","leaderName","leaderParty_label","leaderParty","leaderTitle","location_label","location","maximumElevation","mayor_label","mayor","minimumElevation","motto","municipality_label","municipality","part_label","part","percentageOfAreaWater","populationAsOf","populationDensity","populationMetro","populationMetroDensity","populationRural","populationTotal","populationTotalRanking","populationUrban","populationUrbanDensity","postalCode","region_label","region","state_label","state","synonym","thumbnail_label","thumbnail","timeZone_label","timeZone","twinCity_label","twinCity","twinCountry_label","twinCountry","type_label","type","utcOffset","point","22-rdf-syntax-ns#type_label","22-rdf-syntax-ns#type","wgs84_pos#lat","wgs84_pos#long","depiction_label","depiction","homepage_label","homepage","name","nick" 
"http://dbpedia.org/resource/Kud","Kud","Kud is a town and a notified area committee in Udhampur District in the Indian state of Jammu and Kashmir.","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","India","http://dbpedia.org/resource/India","NULL","NULL","NULL","NULL","NULL","NULL","1855.0","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","{Jammu and Kashmir|Udhampur district}","{http://dbpedia.org/resource/Jammu_and_Kashmir|http://dbpedia.org/resource/Udhampur_district}","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","1140","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","Indian Standard Time","http://dbpedia.org/resource/Indian_Standard_Time","NULL","NULL","NULL","NULL","NULL","NULL","+5:30","33.08 75.28","{city|place|populated place|municipality|City|Place|_Feature|owl#Thing}","{http://dbpedia.org/ontology/City|http://dbpedia.org/ontology/Place|http://dbpedia.org/ontology/PopulatedPlace|http://dbpedia.org/ontology/Settlement|http://schema.org/City|http://schema.org/Place|http://www.opengis.net/gml/_Feature|http://www.w3.org/2002/07/owl#Thing}","33.08","75.28","NULL","NULL","NULL","NULL","Kud","NULL" 

"http://dbpedia.org/resource/Kuju,_Hazaribag","Kuju Hazaribag","Kuju is a census town in Ramgarh district in the Indian state of Jharkhand.","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","India","http://dbpedia.org/resource/India","NULL","NULL","NULL","NULL","NULL","NULL","426.0","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","{Jharkhand|Ramgarh district}","{http://dbpedia.org/resource/Jharkhand|http://dbpedia.org/resource/Ramgarh_district}","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","18049","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","Indian Standard Time","http://dbpedia.org/resource/Indian_Standard_Time","NULL","NULL","NULL","NULL","NULL","NULL","+5:30","23.72 85.5","{city|place|populated place|municipality|City|Place|_Feature|owl#Thing}","{http://dbpedia.org/ontology/City|http://dbpedia.org/ontology/Place|http://dbpedia.org/ontology/PopulatedPlace|http://dbpedia.org/ontology/Settlement|http://schema.org/City|http://schema.org/Place|http://www.opengis.net/gml/_Feature|http://www.w3.org/2002/07/owl#Thing}","23.72","85.5","NULL","NULL","NULL","NULL","Kuju","NULL" 

回答

0

您反覆嘗試從文件中讀取,但一旦文件已經到達末端,你需要再次明確「倒帶」的文件開始到能夠再次讀取相同的數據。

你可以用g.seek(0)來做到這一點,但重新讀取整個文件效率不高。

倒置你的循環,而不是,閱讀一旦

dict_1 = {} 
for row in files: 
    for count, name in enumerate(filenames): 
     key = 'filename{}'.format(count) 
     value = row[name] 
     dict_1.setdefault(key, []).append(value) 

我換成你的手冊count_2enumerate()功能遞增。

+0

非常感謝!我並不知道需要重放一個文件,作爲python和編程的新手。真的很感激它! – AndrewF