求助,刚学python,有个问题无法解决!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求助,刚学python,有个问题无法解决!相关的知识,希望对你有一定的参考价值。

使用python 2.7版本!
源码:
# -*- coding: utf-8 -*-
import csv
from io import open

g_record = [];

a = [1,2,3];
b = [2,5,5];
g_record.append(a);
g_record.append(b);

fd = open("newrecord.csv","w+")
writer = csv.writer(fd)
for line in g_record:
writer.writerow(line);
fd.close()

错误:
Traceback (most recent call last):
File "Script1.py", line 15, in <module>
writer.writerow(line);
TypeError: must be unicode, not str

参考技术A fd = open("newrecord.csv","w+")
改成
fd = open("newrecord.csv","wb+")
用二进制方式打开就ok了
参考技术B # -*- coding: utf-8 -*-
import csv
from io import open

g_record = []

a = [1,2,3]
b = [2,5,5]
g_record.append(a)
g_record.append(b)

fd = open("newrecord.csv","wb")
writer = csv.writer(fd)
for line in g_record:
print line
writer.writerow(line)
fd.close()
在python2.6下通过本回答被提问者和网友采纳

以上是关于求助,刚学python,有个问题无法解决!的主要内容,如果未能解决你的问题,请参考以下文章

刚学前端,最近遇到一个jQuery的问题,还请路过的大神帮忙解决下

新人求助,打包WEBGL怎么运行

从零起步学python计划及感想

多面手遇到小公司

刚学Python,遇到No Python Interpreter configured for the project,怎么解决?

Python 连接 Redis 报错,求助