1

I'm using Python 2.7

Here I create a set of dictionaries:

    day0 = 0
    day1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
            11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
            21, 22, 23, 24, 25]

    month0 = 0
    december = [day0, day1]

    calendar = [month0, december]

Then what I want to do is this:

    file = open("calendarScript.py", "w")
    file.write(calendar) ## Trying to create the calendar in a new doc
    file.close()

But I get this error:

    TypeError: expected a string or other character buffer object

Is there a way to recreate a dictionary in a new document? Thank you for your help :)

P.s., I just tried this:

    import shutil

    shutil.copy(calendar, newFolder)

And got back this error:

    TypeError: coercing to Unicode: need string or buffer, list found

Trying to find a way to copy a dict to a new file.

travis
  • 146
  • 12
  • I don't think you have any idea what a dictionary is. – jonrsharpe Mar 07 '16 at 20:30
  • First you have to read about what a [dictionary](https://docs.python.org/2/tutorial/datastructures.html#dictionaries) actually is – Idos Mar 07 '16 at 20:32
  • I learned from Zed Shaw in Learn Python the Hard Way (http://learnpythonthehardway.org/book/ex39.html). I thought that a dict was something like this: calendar = [month0, december]... Which could then be called like this: calendar[1]... Then I would get the contents of december. – travis Mar 07 '16 at 20:35
  • You have `int`s and `list`s in your code. You do not have any `dict`s – inspectorG4dget Mar 07 '16 at 20:35

1 Answers1

0

The answer to my problem was "dump". What I was trying to do was "dump" to a text file. Thanks to @KFL for this response (link below):

Writing a dict to txt file and reading it back?

    >>> import json
    >>> d = {"one":1, "two":2}
    >>> json.dump(d, open("text.txt",'w'))

He also answered what was going to be my next problem:

    >>> d2 = json.load(open("text.txt"))
    >>> print d2
    {u'two': 2, u'one': 1}
Community
  • 1
  • 1
travis
  • 146
  • 12