Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JSON转换为.ics(Python)_Python_Json_Calendar_Icalendar - Fatal编程技术网

将JSON转换为.ics(Python)

将JSON转换为.ics(Python),python,json,calendar,icalendar,Python,Json,Calendar,Icalendar,我正在尝试将JSON文件转换为iCalendar文件。我的主管建议使用两个函数convertTo(data)(将JSON转换为字符串)和convertFrom(data)(将字符串转换为JSON;我不确定此函数的用途) 我目前的方法使用了很多重构和多个函数 #returns a String def __convert(data): convStr = __convertTo(data) convStr = __fields(convStr) return convStr

我正在尝试将JSON文件转换为iCalendar文件。我的主管建议使用两个函数convertTo(data)(将JSON转换为字符串)和convertFrom(data)(将字符串转换为JSON;我不确定此函数的用途)

我目前的方法使用了很多重构和多个函数

#returns a String
def __convert(data):
    convStr = __convertTo(data)
    convStr = __fields(convStr)
    return convStr

#convert JSON to a String
def __convertTo(data):
    str = "" + data
    return str

#takes string arg (prev converted from JSON) to split it into useful info
def __fields(data)
    #########
    iCalStr = __iCalTemplate(title, dtStart, UID, remType, email)
    return iCalStr

#
def __iCalTemplate(title, dtStart, UID, remType, email):
    icsTempStr = "BEGIN:VEVENT\n
                  DTSTART:" + dtStart + "\nUID:" + UID + "\nDESCRIPTION:" + desc + "\nSUMMARY:" + title
    if remType is not None
        icsTempStr += "\nBEGIN:VALARM\nACTION:" + remType + "DESCRIPTION:This is an event reminder"
        if remType is email
            icsTempStr += "\nSUMMARY:Alarm notification\nATTENDEE:mailto:" + email
        icsTempStr += "\nEND:VALARM"
return icsTempStr

任何提示或建议都会非常有用。我完全知道这段代码需要做大量的工作。

这不是一个完整的答案,而是一个较长的提示

有一个Python习惯用法对您构建字符串非常有帮助,特别是潜在的大型字符串。看一个例子可能比解释更容易:

>>> template = 'a value: {a}; b value: {b}'
>>> data = {'a': 'Spam', 'b': 'Eggs'}
>>> template.format(**data)
'a value: Spam; b value: Eggs'
与字符串连接相比,这种习惯用法有许多优点,如果您正确编写模板,则可以完全消除对函数的需要。例如,可以为可选插入指定值“”。一旦您正确格式化了iCal模板,只需从JSON中检索正确的数据点。。。如果您将模板插入点命名为与JSON中相同的名称,您甚至可以在一个步骤中完成转换。经过一点计划,您的最终答案可能很简单:

import json
template = 'full iCal template with {insert_point} spec goes here'
data = json.JSONDecoder().decode(your_json_data)
ical = template.format(**data)
要执行一个快速(略有不同)的解释器示例:

>>> import json
>>> decoder = json.JSONDecoder()
>>> json_example = '{"item_one" : "Spam", "item_two" : "Eggs"}'
>>> template = 'Item 1: {item_one}\nItem 2: {item_two}'
>>> print template.format(**decoder.decode(json_example))
Item 1: Spam
Item 2: Eggs

我最终使用了一种完全不同的、更有效的方法来实现这一点。总之,我的方法遍历JSON,从每个字段提取每个值,并手动将其放置在iCalendar模板中的适当位置。它返回一个字符串。像这样的

def convert(self, json):
    template = 'BEGIN:VEVENT\n'
    template += 'DTSTART:%s\n' % json['event-start']
    ...
    return template

这个问题可以通过增加一个例子变得更加清楚;您希望将什么JSON对象转换为什么ics输出?您是在征求代码建议,还是有具体的错误消息或不当行为?你的代码可以编译吗?很难确切知道你在这里做什么。。。但是_fields()接受一个数据参数,然后对它不做任何处理。它使用您包含的代码中未定义的多个参数调用_iCalTemplate。__iCalTemplate()是否针对某些测试数据工作?如果是这样,请发布测试数据和JSON数据的示例。如果不是。。。首先解决这个问题。我已经尝试实现了这种方法,而不是以前所做的,但是在使用decode()时遇到了很多问题。当我尝试json.decode()时,会收到一条错误消息,说“module”对象没有属性“decode”。当我尝试json.JSONDecoder().decode()时,我收到一条错误消息,读取“/usr/lib/python2.7/json/decoder.py,第366行end=self.raw_decode(s,idx=_w(s,o.end());TypeError:预期的字符串或缓冲区。不知道如何解释最后一个。抱歉……decode()是JSONDecoder对象的一个方法。我将更新答案并提供一个快速示例。