我幾乎沒有 Python 編程經驗,但需要將當前時間以某種 12 小時格式從幾個不同的時區匯出到單個文本檔案。我找到了幾種解決方案,但需要將所有三個組件集成到一個解決方案中。
from datetime import datetime
"{:%A, %B %d %Y %I:%M:%S %p %Z}".format(datetime.now())
import datetime
import pytz
my_date = datetime.datetime.now(pytz.timezone('UTC'))
print(my_date)
my_date = datetime.datetime.now(pytz.timezone('America/New_York'))
print(my_date)
my_date = datetime.datetime.now(pytz.timezone('EST'))
print(my_date)
my_date = datetime.datetime.now(pytz.timezone('Pacific/Port_Moresby'))
print(my_date)
import datetime
open("C:\temp\current_time.txt", "w").write("UTC Time: ")
open("C:\temp\current_time.txt", "a").write(datetime.datetime.now().ctime() '\n')
open("C:\temp\current_time.txt", "a").write("EST Time: ")
open("C:\temp\current_time.txt", "a").write(datetime.datetime.now().ctime() '\n')
open("C:\temp\current_time.txt", "a").write("New York Time: ")
open("C:\temp\current_time.txt", "a").write(datetime.datetime.now().ctime() '\n')
open("C:\temp\current_time.txt", "a").write("Port Moresby Time: ")
open("C:\temp\current_time.txt", "a").write(datetime.datetime.now().ctime())
最后一個序列似乎在 Python 3.7 中不起作用,但如果可能的話,希望它使用(不幸的是)Python 2.7 安裝來作業。
最終,我想要這些結果:
UTC Time: Monday, October 11, 2021 09:05:17
EST Time: Monday, October 11, 2021 04:05:17 AM
New York Time: Monday, October 11, 2021 05:05:17 AM
Port Moresby Time: Monday, October 11, 2021 07:05:17 PM
非常感謝您的任何幫助。
uj5u.com熱心網友回復:
您的代碼有幾個問題:
- 您正在呼叫
datetime.datetime.now四次,這將在執行這些陳述句的時刻為您提供日期時間,即您將在四個不同的時刻獲得日期時間。您只需要獲取當前時刻的日期時間一次,然后將其轉換為所需時區中的相應日期時間。 - 我建議您使用
with open(...) as variable塊,它使您的代碼能夠在退出塊后立即自動關閉檔案。
使用 Python 3.9 測驗的作業代碼:
from datetime import datetime
import pytz
now = datetime.now()
now_utc = now.astimezone(pytz.timezone('UTC'))
now_est = now.astimezone(pytz.timezone('EST'))
now_new_york = now.astimezone(pytz.timezone('America/New_York'))
now_port_moresby = now.astimezone(pytz.timezone('Pacific/Port_Moresby'))
format = "%A, %B %d, %Y %I:%M:%S %p"
with open("current_time.txt", "w") as file:
file.write(f'UTC Time: {now_utc.strftime(format)}\n')
file.write(f'EST Time: {now_est.strftime(format)}\n')
file.write(f'New York Time: {now_new_york.strftime(format)}\n')
file.write(f'Port Moresby Time: {now_port_moresby.strftime(format)}\n')
從示例運行寫入 current_time.txt 的資料:
UTC Time: Monday, October 11, 2021 12:25:50 PM
EST Time: Monday, October 11, 2021 07:25:50 AM
New York Time: Monday, October 11, 2021 08:25:50 AM
Port Moresby Time: Monday, October 11, 2021 10:25:50 PM
使用 Python 2.7 測驗的作業代碼:
from datetime import datetime
import pytz
now_utc = datetime.utcnow()
tz_utc = pytz.timezone('UTC')
now_est = tz_utc.localize(now_utc).astimezone(pytz.timezone('EST'))
now_new_york = tz_utc.localize(now_utc).astimezone(pytz.timezone('America/New_York'))
now_port_moresby = tz_utc.localize(now_utc).astimezone(pytz.timezone('Pacific/Port_Moresby'))
format = "%A, %B %d, %Y %I:%M:%S %p"
with open("current_time.txt", "w") as file:
file.write('UTC Time: ' now_utc.strftime(format) '\n')
file.write('EST Time: ' now_est.strftime(format) '\n')
file.write('New York Time: ' now_new_york.strftime(format) '\n')
file.write('Port Moresby Time: ' now_port_moresby.strftime(format) '\n')
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/312265.html
標籤:Python python-2.7 约会时间 格式 时区
下一篇:回圈給出多個布林值
