csv中的未知错误dict并列出转换解析器

时间:2016-11-27 10:24:16

标签: python list loops csv dictionary

我已经使用DictReader函数将csv文件加载到我的程序中。我的任务是将csv文件的每一列附加到标有“filename”的单独列表中加上计数。一旦转换成字典,我就使用这段代码,但它只将第一个临时列表附加到我的最终字典中。有人可以在这里指出这个问题吗?

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"

1 个答案:

答案 0 :(得分: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_2函数替换了手动enumerate()递增。