【问题标题】:read text file content with python at zapier在 zapier 使用 python 读取文本文件内容
【发布时间】:2018-01-06 07:46:48
【问题描述】:

我在将 txt 文件的内容导入 Zapier 时遇到问题 对象使用https://zapier.com/help/code-python/。这是我的代码 使用:

with open('file', 'r') as content_file:
content = content_file.read()

如果你能帮我解决这个问题,我会很高兴。谢谢!

【问题讨论】:

    标签: python-2.7 zapier


    【解决方案1】:

    David 来自 Zapier 平台团队。

    您编写的代码不起作用,因为open 函数的第一个参数是文件路径。路径'file' 没有文件,所以你会得到一个错误。您可以通过 input_data 字典访问输入。

    话虽如此,输入是一个 url,而不是一个文件。您需要使用urllib 来阅读该网址。我找到了答案here

    我有一个这样的代码的工作副本:

    import urllib2  # the lib that handles the url stuff
    result = []
    
    data = urllib2.urlopen(input_data['file'])
    for line in data:            # file lines are iterable
        result.append(line)      # keep each line, or parse, etc.
    
    return {'lines': result}
    

    关键是您需要从函数中返回一个字典,因此请确保您以某种方式将文件压缩为一个。

    ​如果您还有其他问题,请告诉我!

    【讨论】:

      【解决方案2】:

      @xavid,你在 Zapier 中测试过这个吗?

      因为 zapier python 环境中不存在 urllib2,所以失败得很惨。

      【讨论】:

        猜你喜欢
        • 2021-07-18
        • 2021-05-05
        • 2016-10-14
        • 1970-01-01
        • 2020-12-24
        • 1970-01-01
        • 2020-09-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多