0

I'm trying to withdraw all the tasks from a specific project within Todoist using their API and Python.

My code looks like this:

ListOfProjects = api.get_projects()

ListOfPeople = api.get_tasks(project_id = 1234567899,)

file = open('outputa.txt', 'w', encoding="utf-8")

print(ListOfPeople, file = file)

file.close()

input("Press Enter To Exit")

This then prints the JSON Encoded information to said file, for example:

[
    Task(
        id: 2995104339,
        project_id: 2203306141,
        section_id: 7025,
        parent_id: 2995104589,
        content: 'Buy Milk',
        description: '',
        comment_count: 10,
        assignee: 2671142,
        assigner: 2671362,
        order: 1,
        priority: 1,
        url: 'https://todoist.com/showTask?id=2995104339'
    ...
)

This gives me a massive, unwieldy text document (as there is one of the above for every task in a nearly 300 task project). I just need the string after the Content parameter.

Is there a way to specify that just the Content parameter should be printed?

accdias
  • 5,160
  • 3
  • 19
  • 31

1 Answers1

0

According to documentation, get_tasks method returns a list of Task object, so if you need to store just content property for each of them, you can change your code like this:

data = [t.content for t in api.get_tasks(project_id = 1234567899,)]
with open('outputa.txt', 'w', encoding="utf-8") as f:
    print(data, file=f)
svfat
  • 3,273
  • 1
  • 15
  • 34
  • Perfect! That works great. If i may ask, what does this line do?: `data = [t.content for t in api.get_tasks(project_id = 1234567899,)]` – Oliver Fitzgerald Jul 12 '22 at 15:23
  • 1
    @Oliver: That line is assigning the result of a [list comprehension](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) to the variable `data`. – martineau Jul 12 '22 at 15:39
  • Hi again @martineau, I'm trying to do a similar thing further down in my code where I create a new project, assign it to a variable, and then try and pull the project id from that code to assign that to a variable. However, when I try and use, for example `t.id`, it doesn't see it as a variable to be extracted, the way it did with `t.content`. Here is the code: `newproject = api.add_project(name='Ideas for the Schedule EDIT') for t in api.get_project(newproject): newprojectid = t.id in newproject` – Oliver Fitzgerald Jul 13 '22 at 09:51
  • @Oliver: As far as I can tell, that code is not a list comprehension. I also think you would need to call `api.get_projects()`, not `api.get_project()` if you want to iterate over a series of project objects. – martineau Jul 13 '22 at 14:22