[英]Logging to two CSV files in parallel (python)
我正在讀取一系列氣候數據文本文件,每個文件包含9000行,第一行是氣象站的信息,例如名稱,經度等。 從8到末端的線包含氣候數據。
我想讀取所有文件並輸出兩個文件“ MetLocations.csv”和“ MetData.csv”,其中第一個文件包含所有文件的第一行的集合,而第二個文件包含所有氣候數據。
我遍歷了所有文件,閱讀了信息,並毫無疑問地打印了它們,除了一個奇怪的問題。 當我進行兩次“ with”循環時,文件MetLocations.csv留為空白,而當我逐次進行循環處理時,它寫得很好! 請查看代碼以獲得更清晰的視圖。
這是我的代碼的一部分:
with open('MetLocations.csv','wb+') as locations:
wr_loc = csv.writer(locations, quoting=csv.QUOTE_NONE, quotechar='', delimiter=',', escapechar='\\')
headers = ["St_Id","City","State","Country","Source","WO code","Lat","Long","TimeZone","Alt"]
wr_loc.writerow(headers)
with open('MetData.csv','a+')as met_data:
wr_data = csv.writer(met_data, quoting=csv.QUOTE_NONE, quotechar='', delimiter=',', escapechar='\\',lineterminator='\n')
headers = ["St_ID", "Year", "Month", "Day", "Hour", "Minute", "Data Source and Uncertainty Flags", "Dry Bulb Temperature"]
wr_data.writerow(headers)
for n, f in enumerate(filenames):
print n, f,time.strftime('%Y-%m-%d %H:%M:%S')
full_path = os.path.join(source, f)
with open(full_path, 'r') as input_file:
content = input_file.readlines()
location = [n] + list_elements(content[0])[1:]
wr_loc.writerow(location)
meteo = []
for line in content[8:]:
meteo.append([n] + list_elements(line))
wr_data.writerows(meteo)
在運行上述代碼的最后,我發現文件MetData.csv充滿了數據,而文件MetLocations.csv為空!
奇怪的是,當我運行相同的代碼並注釋了最后4行底行時,MetLocations.csv會充滿問題!
是否不允許打開兩個連續的CSV文件進行追加? 或我的代碼中錯誤的部分是什么?
非常感謝您的幫助。
您需要在同一行中打開所有文件,以便成為同一上下文的一部分:
with open('MetLocations.csv','wb+') as locations, open('MetData.csv','w')as met_data:
wr_loc = csv.writer(locations,
quoting=csv.QUOTE_NONE,
quotechar='', delimiter=',', escapechar='\\')
wr_data = csv.writer(met_data,
quoting=csv.QUOTE_NONE,
quotechar='', delimiter=',',
escapechar='\\',lineterminator='\n')
headers = ["St_Id","City","State","Country","Source","WO code","Lat","Long","TimeZone","Alt"]
wr_loc.writerow(headers)
headers = ["St_ID", "Year", "Month", "Day", "Hour", "Minute", "Data Source and Uncertainty Flags", "Dry Bulb Temperature"]
wr_data.writerow(headers)
for n, f in enumerate(filenames):
print n, f,time.strftime('%Y-%m-%d %H:%M:%S')
full_path = os.path.join(source, f)
with open(full_path, 'r') as input_file:
# your code here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.