使用python程式,我將一個ics檔案保存到一個json檔案中。json檔案包含日歷資訊。我的目的是用不同的字串(關鍵字)替換一些特定的字串(小時)。基本上8:00到Meeting1;9:00 到會議 2,依此類推。Json 內容看起來像這樣11/18/21 09:00 UTC-12/19/25 09:45 UTC Meeting-boss: - None。這是由 python 程式完成的,更改可能會很痛苦,所以我必須使用它。這是將 ics 檔案決議為 json 檔案的 python 程式:
from datetime import datetime, timedelta, timezone
import icalendar
from dateutil.rrule import *
f = open('myschool.json', 'w')
def parse_recurrences(recur_rule, start, exclusions):
""" Find all reoccuring events """
rules = rruleset()
first_rule = rrulestr(recur_rule, dtstart=start)
rules.rrule(first_rule)
if not isinstance(exclusions, list):
exclusions = [exclusions]
for xdate in exclusions:
try:
rules.exdate(xdate.dts[0].dt)
except AttributeError:
pass
now = datetime.now(timezone.utc)
this_year = now timedelta(days=60)
dates = []
for rule in rules.between(now, this_year):
dates.append(rule.strftime("%D %H:%M UTC "))
return dates
icalfile = open('myschool.ics', 'rb')
gcal = icalendar.Calendar.from_ical(icalfile.read())
for component in gcal.walk():
if component.name == "VEVENT":
summary = component.get('summary')
description = component.get('description')
location = component.get('location')
startdt = component.get('dtstart').dt
enddt = component.get('dtend').dt
exdate = component.get('exdate')
if component.get('rrule'):
reoccur = component.get('rrule').to_ical().decode('utf-8')
for item in parse_recurrences(reoccur, startdt, exdate):
print("{0} {1}: {2} - {3}\n".format(item, summary, description, location), file = f)
else:
print("{0}-{1} {2}: {3} - {4}\n".format(startdt.strftime("%D %H:%M UTC"), enddt.strftime("%D %H:%M UTC"), summary, description, location), file = f)
icalfile.close()
我不知道如何做到這一點。順便說一句,json 可以是一個 txt 檔案,如果它使事情更容易的話。感謝所有幫助:)
uj5u.com熱心網友回復:
我認為您可以使用“re”包(python正則運算式)來做到這一點,然后您可以使用方法replace將目標字串替換為所需的字串
您可以在此處閱讀有關re 模塊的更多資訊https://docs.python.org/3/library/re.html
這個站點真的會幫助你搜索 JSON 檔案并找到目標字串https://pythex.org/
uj5u.com熱心網友回復:
如果沒有示例 icalendar 組件與當前 json 輸出以及您預期的 json 輸出一起,您的代碼有點難以理解。如果我正確解釋了您的代碼,看起來您正在通過使用 component.get() 格式化從 icalendar 組件檢索到的資料來構建 json。如果這是正確的,您應該能夠在第一次從保存此字串的變數中將時間部分(在您的示例中為 8:00 和 9:00)替換為“會議 1”和“會議 2”日歷檔案。然后,您可以像當前一樣從字串中組裝您的 json,因為您想要進行的更改將在構建您的 json 檔案之前完成。
例如:如果 startdt = component.get('dtstart').dt 保存了從 icalendar 中提取的對應于“11/18/21 09:00”的字串,您將使用正則運算式或字串拆分之類的東西來洗掉“09: 00”并添加“會議 2”。一旦變數 startdt 被修改為擁有您想要的資料,您的 json 就應該繼承這些更改。
假設 startdt 包含為您的 json 提供日期和時間的字串(例如:“11/18/21 09:00”),您可以使用以下代碼將字串更改為“11/18/21 Meeting 2”。
# import regex library
import re
# takes place of startdt = component.get('dtstart').dt in your code to make example easier.
startdt = "11/18/21 09:00"
# uses regex patter that looks for 2 digits, then ":" followed by 2 digits,
#then replaces the string that matches that pattern with "Meeting 2"
startdt = startdt.replace(re.search('\d{2}:\d{2}', startdt)[0], "Meeting 2")
print(startdt)
您沒有指定如何在替換期間決定如何命名每個會議,但您可以將任何您想要的名稱傳遞給 startdt.replace() 行,而不是示例中的硬編碼“會議 2”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/375237.html
上一篇:通過帶有嵌套陣列的mongo-drivergolang查找檔案
下一篇:多行到PHP關聯陣列
