Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Python Question
-
Greetings,
I have a list of emails that I'm trying to separate it into a few lists of just a couple emails each.
To begin with, I have a text file with one email per line, no separators. My python/workflow prowess is good enough to add quotation marks before and after each line as well as a comma after each line, then copy it all to the clipboard.
Then I can flip over to the console and set a variable to point to a list with the emails in it (all_emails = [paste]).
Finally I can pull portions of the list (some_emails = all_emails[0:3]). It feels like I'm on the right track.
However, when I try to accomplish the same thing, using a script instead of the console, it gives me an error message saying that it needs a string. So I tried to turn some_emails into a string and it gives me the whole list, not just the first couple of emails.
The list is not being created in the script like it is in the console. I suspect it has to do with encoding, but I don't have the first clue how to fix it. :-) Remember it pulls the contents of a text file with one email per line and no separators. I'll post a link to the workflow so you can see what's happening:
http://www.editorial-workflows.com/workflow/5320493146046464/V8Ii1guwpB8
I would be super grateful for your help!
Thanks!
-
First, remove both prefix/suffix actions, you don't need those to turn the email addresses into a list. Then you can simply use:
emails = workflow.get_input().splitlines()
It would also be possible to keep the prefix/suffix actions and use
emails = eval('[' + workflow.get_input() + ']')
(which interprets the text as Python list syntax) , but that's a more fragile approach, and I wouldn't recommend it. -
Ole,
That totally worked! Thanks so much!
Dan
-
One more question. Is it possible to use the workflow.set_variable() action to set more than one variable in a single script? If so, how would I call those variables in my workflow?
Thanks again!
-
Yes, that's possible, but due to the way the variable picker works, variables have to be "declared" in order to be usable in the rest of the workflow. Basically, you need to have a Set Variable action for every variable you want to use outside of a script (it can just set an empty value that you change later via Python).
If you really want, you can actually delete the Set Variable action after inserting it where you want.
-
Nice. I'll give that a run.
-
Here's what I came up with:
http://www.editorial-workflows.com/workflow/5320493146046464/V8Ii1guwpB8
It's saying that emails-one is not defined. I'm assuming my syntax is off in the set-variable statement?
Thanks!
-
You have to put the variable name in quotation marks:
#... workflow.set_variable('emails_one', str(emails[0:3]))
-
Btw, if you want the contents of the variable to be one email address per line (like the input), use
'\n'.join(emails[0:3])
instead ofstr(...)
. -
One email per line isn't necessary. I'm heading to bed now. I'll give it another run in the morning. Thanks so much Ole!
-
Finished it up. Workflow is here. Thanks so much for your help on this Ole!