使用 for 循环从列表(包括子列表)中提取元素,创建子列表并将其导出到 CSV 文件,Python

Extracting elements from a list (inc sub-lists) with a for loop, creating a sublist and exporting it to CSV file, Python

我有一个名为 data_all 的元素列表(一些元素包含子列表),即

data_all=[{u'heatindexm': u'-9999', u'windchillm': u'-999', u'wdire': u'SW', u'wdird': u'230', u'windchilli': u'-999', u'hail': u'0', u'heatindexi': u'-9999', u'precipi': u'-9999.00', u'thunder': u'0', u'pressurei': u'29.44', u'snow': u'0', u'pressurem': u'997', u'fog': u'0', u'icon': u'mostlycloudy', u'precipm': u'-9999.00', u'conds': u'Mostly Cloudy', u'tornado': u'0', u'hum': u'82', u'tempi': u'50.0', u'tempm': u'10.0', u'dewptm': u'7.0', u'rain': u'0', u'dewpti': u'44.6', u'date': {u'mday': u'11', u'hour': u'00', u'min': u'20', u'mon': u'05', u'pretty': u'12:20 AM BST on May 11, 2014', u'year': u'2014', u'tzname': u'Europe/London'}, u'visi': u'6.2', u'vism': u'10.0', u'utcdate': {u'mday': u'10', u'hour': u'23', u'min': u'20', u'mon': u'05', u'pretty': u'11:20 PM GMT on May 10, 2014', u'year': u'2014', u'tzname': u'UTC'}, u'wgusti': u'-9999.0', u'metar': u'METAR EGBB 102320Z 23009KT 200V260 9999 SCT021 BKN027 10/07 Q0997', u'wgustm': u'-9999.0', u'wspdi': u'10.4', u'wspdm': u'16.7'}, {u'heatindexm': u'-9999', u'windchillm': u'-999', u'wdire': u'SW', u'wdird': u'230', u'windchilli': u'-999', u'hail': u'0', u'heatindexi': u'-9999', u'precipi': u'-9999.00', u'thunder': u'0', u'pressurei': u'29.44', u'snow': u'0', u'pressurem': u'997', u'fog': u'0', u'icon': u'mostlycloudy', u'precipm': u'-9999.00', u'conds': u'Mostly Cloudy', u'tornado': u'0', u'hum': u'82', u'tempi': u'50.0', u'tempm': u'10.0', u'dewptm': u'7.0', u'rain': u'0', u'dewpti': u'44.6', u'date': {u'mday': u'11', u'hour': u'00', u'min': u'50', u'mon': u'05', u'pretty': u'12:50 AM BST on May 11, 2014', u'year': u'2014', u'tzname': u'Europe/London'}, u'visi': u'6.2', u'vism': u'10.0', u'utcdate': {u'mday': u'10', u'hour': u'23', u'min': u'50', u'mon': u'05', u'pretty': u'11:50 PM GMT on May 10, 2014', u'year': u'2014', u'tzname': u'UTC'}, u'wgusti': u'-9999.0', u'metar': u'METAR EGBB 102350Z 23010KT 200V270 9999 BKN024 BKN033 10/07 Q0997', u'wgustm': u'-9999.0', u'wspdi': u'11.5', u'wspdm': u'18.5'}, {u'heatindexm': u'-9999', u'windchillm': u'-999', u'wdire': u'WSW', u'wdird': u'240', u'windchilli': u'-999', u'hail': u'0', u'heatindexi': u'-9999', u'precipi': u'-9999.00', u'thunder': u'0', u'pressurei': u'29.44', u'snow': u'0', u'pressurem': u'997', u'fog': u'0', u'icon': u'cloudy', u'precipm': u'-9999.00', u'conds': u'Overcast', u'tornado': u'0', u'hum': u'82', u'tempi': u'50.0', u'tempm': u'10.0', u'dewptm': u'7.0', u'rain': u'0', u'dewpti': u'44.6', u'date': {u'mday': u'11', u'hour': u'01', u'min': u'20', u'mon': u'05', u'pretty': u'1:20 AM BST on May 11, 2014', u'year': u'2014', u'tzname': u'Europe/London'}, u'visi': u'5.0', u'vism': u'8.0', u'utcdate': {u'mday': u'11', u'hour': u'00', u'min': u'20', u'mon': u'05', u'pretty': u'12:20 AM GMT on May 11, 2014', u'year': u'2014', u'tzname': u'UTC'}, u'wgusti': u'-9999.0', u'metar': u'METAR EGBB 110020Z 24010KT 210V270 8000 BKN018 OVC025 10/07 Q0997', u'wgustm': u'-9999.0', u'wspdi': u'11.5', u'wspdm': u'18.5'}]

注意:我在上面只显示了三个元素,实际列表超过 1,000 个元素

我正在使用 for 循环从列表中包含的元素中提取感兴趣的值,将这些感兴趣的元素传递到单独的列表,然后使用以下代码将其导出为 CSV 文件:

#define output file location and create empty list
import csv
Output_file="outputfile.csv"
interesting_data = []
#I then implement a for loop so that the values of interest from each element in the data_all list
#can be obtained
for values in data_all:
    data_string_sample=((values['utcdate']['mday']),(values['utcdate']['mon']),(values['utcdate']['year']),(values['utcdate']['hour']),(values['utcdate']['min']),(values['tempm']),(values['hum']),(values['pressurem']))
    interesting_data.append(data_string_sample)
    #append elements of interest onto the new list, interesting_data
    #pushing the list to the csv file by way of csv.writer
    #NB specfiting ab instead of wb so the data is appended to the CSV file instead of overwriting it
    #on subsequent passes
    with open(Output_file, "ab") as resultFile:
        #Override the default white space generator ('\r\n')
        #and specfiying a new lineterminator of '\n' so that each
        #list element starts on the next sequential row instead
        #of skipping a row
        writer = csv.writer(resultFile, lineterminator='\n')
        writer.writerows(interesting_data)
#end code

我的问题是,csv 文件没有显示 3 行数据(即 interesting_data 中的元素),它似乎将第一个元素作为第一行添加到数据文件中,再次重复循环但将元素一添加为第 2 行,将元素 2 添加为第 3 行, 再次重复循环并添加元素 1 作为 row4,元素 2 作为 row5,元素 3 作为 row6 等等以产生 6 行,即 csv 文件输出如下所示:

10  5   2014    23  20  10  82  997
10  5   2014    23  20  10  82  997
10  5   2014    23  50  10  82  997
10  5   2014    23  20  10  82  997
10  5   2014    23  50  10  82  997
11  5   2014    0   20  10  82  997

虽然我希望它看起来像:

10  5   2014    23  20  10  82  997
10  5   2014    23  50  10  82  997
11  5   2014    0   20  10  82  997

我知道我在正确的轨道上,因为 interesting_data 有我想要的元素

>>>interesting_data
[(u'10', u'05', u'2014', u'23', u'20', u'10.0', u'82', u'997'), (u'10', u'05', u'2014', u'23', u'50', u'10.0', u'82', u'997'), (u'11', u'05', u'2014', u'00', u'20', u'10.0', u'82', u'997')]

任何帮助/见解将不胜感激!

将文件写入部分代码移出 for 循环。

for values in data_all:
    ........
    ........
with open(Output_file, "ab") as resultFile:
    ........
    ........