2

I have problems getting the content of a txt-file into a Zapier object using https://zapier.com/help/code-python/. Here is the code I am using:

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

I'd be glad if you could help me with this. Thanks for that!

enter image description here

Robin Alexander
  • 984
  • 2
  • 13
  • 29

2 Answers2

2

David here, from the Zapier Platform team.

Your code as written doesn't work because the first argument for the open function is the filepath. There's no file at the path 'file', so you'll get an error. You access the input via the input_data dictionary.

That being said, the input is a url, not a file. You need to use urllib to read that url. I found the answer here.

I've got a working copy of the code like so:

import urllib  # the lib that handles the url stuff
result = []

data = urllib.request.urlopen(input_data['file'])
for line in data:            # file lines are iterable
    result.append(line)      # keep each line, or parse, etc.

return {'lines': result}

The key takeaway is that you need to return a dictionary from the function, so make sure you somehow squish your file into one.

​Let me know if you've got any other questions!

Community
  • 1
  • 1
xavdid
  • 5,092
  • 3
  • 20
  • 32
1

@xavid, did you test this in Zapier?

It fails miserably beacuse urllib2 doesn't exist in the zapier python environment.

Configuration of Python in Zapier

Error

Alex Sirota
  • 83
  • 1
  • 6