簡體   English   中英

使用for循環從列表(inc子列表)中提取元素,創建子列表並將其導出到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行數據(即,func_data中的元素),它似乎是將第一個元素作為行1添加到數據文件中,再次重復循環,但將元素1添加為row2和element 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
[(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:
    ........
    ........

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM