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

使用for循环从列表(包括子列表)中提取元素,创建子列表并将其导出到CSV文件Python,python,list,for-loop,export-to-csv,Python,List,For Loop,Export To Csv,我有一个元素列表(一些元素包含子列表),称为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

我有一个元素列表(一些元素包含子列表),称为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'}]
注意:我只显示了上面的三个元素,实际列表>1000个元素

我使用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
我的问题是,与显示3行数据的csv文件(即有趣的_数据中的元素)不同,它似乎将第一个元素作为行1添加到数据文件中,再次重复循环,但将元素1添加为行2,将元素2添加为行3, 再次重复循环,并将元素1添加为第4行,元素2添加为第5行,元素3添加为第6行,以此类推,以生成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
[(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:
    ........
    ........

您可以将所有行附加到
感兴趣的\u数据
。某个地方应该有这样一个条件:这是有趣的数据;追加并且不附加其他数据。从您的示例中假设:如果当前行已经在
有趣的\u数据中:不要追加。干杯,我不敢相信我没有看到!!我已经在同一个街区开始很久了:d