小编Kel*_*oll的帖子

2.7 CSV模块需要unicode,但不需要unicode

csvfile_ = open(finishedFileName+num+".csv","w",newline='')
writ = csv.writer(csvfile_, dialect='excel')
firstline = unicode(str(firstline))
try:
    writ.writerow(firstline)
except TypeError:
    print firstline
    print type(firstline)
    raise
Run Code Online (Sandbox Code Playgroud)

我得到了TypeError: must be unicode, not str这个代码.当我打印出第一线的类型时<type 'unicode'>.当我打印第一行时,我看到['project_number', 'project_location'](列表比这长,但它继续以那种风格.)

这个程序在python 3.3中运行良好.我用3to2移植它,从我这样做,从unix切换到windows.

如何使这个程序顺利写入?

注意:根据官方文档,此版本的csv模块不支持Unicode输入,但它告诉我无论如何都要给它输入Unicode.

完全例外

Traceback (most recent call last):
  File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 382, in <module>
    process(marketingLogExportFileName)
  File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 123, in process
    writing(csvfile,modified,firstline)
  File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 114, in writing
    writ.writerow(firstline)
TypeError: must be unicode, not str
Run Code Online (Sandbox Code Playgroud)

如果我拿出代码来制作第一行unicode,我会得到

Traceback (most recent call last):
  File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 382, …
Run Code Online (Sandbox Code Playgroud)

python csv unicode error-handling python-2.7

12
推荐指数
1
解决办法
5808
查看次数

是否可以在Python中反转任意lambda?

我最近一直在玩Python和数学,然后我遇到了一些我还没弄清楚的东西.也就是说,给定一个任意的lambda,是否有可能返回该lambda的逆数进行数学运算?也就是说,invertLambda使得invertLambda(lambda x:(x + 2))(2)= 0. lambdas仅限于表达式的事实给了我希望,但到目前为止我还没有能够使它工作.我知道任何结果都会导致丢失信息的功能出现问题,但如果必须,我愿意将用户和我自己限制在无损功能上.

python math lambda

1
推荐指数
1
解决办法
1285
查看次数

标签 统计

python ×2

csv ×1

error-handling ×1

lambda ×1

math ×1

python-2.7 ×1

unicode ×1