将随机日期写入文件Python

时间:2017-03-31 18:10:03

标签: python fwrite

Python 2.7.13 - 版本

我有一个.py,其中包含一些生成随机信息的方法(日期,姓名,电话号码)。

docCookies.setItem("test0", "Hello world!");
docCookies.setItem("test1", "Unicode test: \u00E0\u00E8\u00EC\u00F2\u00F9", Infinity);
docCookies.setItem("test2", "Hello world!", new Date(2020, 5, 12));
docCookies.setItem("test3", "Hello world!", new Date(2027, 2, 3), "/blog");
docCookies.setItem("test4", "Hello world!", "Wed, 19 Feb 2127 01:04:55 GMT");
docCookies.setItem("test5", "Hello world!", "Fri, 20 Aug 88354 14:07:15 GMT", "/home");
docCookies.setItem("test6", "Hello world!", 150);
docCookies.setItem("test7", "Hello world!", 245, "/content");
docCookies.setItem("test8", "Hello world!", null, null, "example.com");
docCookies.setItem("test9", "Hello world!", null, null, null, true);
docCookies.setItem("test1;=", "Safe character test;=", Infinity);

alert(docCookies.keys().join("\n"));
alert(docCookies.getItem("test1"));
alert(docCookies.getItem("test5"));
docCookies.removeItem("test1");
docCookies.removeItem("test5", "/home");
alert(docCookies.getItem("test1"));
alert(docCookies.getItem("test5"));
alert(docCookies.getItem("unexistingCookie"));
alert(docCookies.getItem());
alert(docCookies.getItem("test1;="));

我得到一个带有def strTimeProp(start, end, format, prop): stime = time.mktime(time.strptime(start, format)) etime = time.mktime(time.strptime(end, format)) ptime = stime + prop * (etime - stime) return time.strftime(format, time.localtime(ptime)) def randomDate(start, end, prop): return strTimeProp(start, end, '%m/%d/%Y %I:%M %p', prop) print randomDate("1/1/2008 12:00 AM", "1/1/2009 12:00 AM", random.random()) f = open("CDR.txt" ,"w") f.write(randomDate) f.close() 的typeError,它期待一个字符串或缓冲区,而不是一个函数。如何将print语句的输出发送到我写入的.txt文件?

1 个答案:

答案 0 :(得分:1)

您正在将函数randomDate传递给f.write而没有任何参数

这将解决您的问题:

f.write(randomDate("1/1/2008 12:00 AM", "1/1/2009 12:00 AM", random.random()))