我使用的是Python 2.7
这里我创建了一组词典:
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]
那我想做的是:
file = open("calendarScript.py", "w")
file.write(calendar) ## Trying to create the calendar in a new doc
file.close()
但是我收到了这个错误:
TypeError: expected a string or other character buffer object
有没有办法在新文档中重新创建字典? 谢谢你的帮助:))
P.s。,我刚试过这个:
import shutil
shutil.copy(calendar, newFolder)
并找回了这个错误:
TypeError: coercing to Unicode: need string or buffer, list found
尝试找到将dict复制到新文件的方法。
答案 0 :(得分:0)
我的问题的答案是“转储”。我试图做的是“转储”到文本文件。感谢@KFL的回复(链接如下):
Writing a dict to txt file and reading it back?
import java.util.*;
import static java.lang.System.out;
public class Lab26 {
public static void main(String[] args) {
}
public static String letterGrade(double grade) {
String a = "A";
String b = "B";
String c = "C";
String d = "D";
String f = "F";
if (grade <= 100 && grade >= 90) {
return a;
} else if (grade < 90 && grade >= 80) {
return b;
} else if (grade < 80 && grade >= 70) {
return c;
} else if (grade < 70 && grade >= 60) {
return d;
} else if (grade < 60) {
return f;
}
}
}
他还回答了我的下一个问题:
>>> import json
>>> d = {"one":1, "two":2}
>>> json.dump(d, open("text.txt",'w'))