Python unicode写入文件在命令行中崩溃,但在IDE中不崩溃 [英] Python unicode write to file crashes in command line but not in IDE

查看:78
本文介绍了Python unicode写入文件在命令行中崩溃,但在IDE中不崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,其中我的Python 2.7.3rc2代码可通过IDE(带有PyDev的Aptana Studio 3)正常运行,但是当我双击.py文件或尝试从Windows命令运行它时崩溃线.

I'm having a problem wherein my Python 2.7.3rc2 code runs fine through an IDE (Aptana Studio 3 with PyDev), but crashes when I either double-click the .py file or try to run it from the Windows command line.

问题所在的行是我尝试将包含Unicode字符的字符串写入文件的地方. IDE没问题,并使用unicode字符正确写入了文件.命令行版本抱怨它无法编码某些字符.

The problem line is where I try to write a string containing unicode characters to a file. The IDE has no problem with it, and writes the file properly with the unicode characters. The command line version complains that it can't encode certain characters.

问题的根源是:IDE版本与命令行版本有什么不同,一个版本正确编写unicode文件,而另一个版本则不行?

The root of the question is: what's different about the IDE version versus the command line version that one writes a unicode file properly and the other does not?

理想的解决方案应该使命令行版本与IDE版本完全一样.

The ideal solution should have the command line version working exactly as the IDE version does.

抱歉,我以为是我在使用哪个命令将字符串写入文件的方法,但是我是Python的新手.实际命令是在用f = open(path, 'w')实例化的对象f上调用的write().我将要写入文件的字符串传递给该字符串,并且该字符串包含unicode字符.

Sorry, I thought it was assumed which command I was using to write a string to a file, but I'm new to Python. The actual command is write() called on an object f which was instantiated with f = open(path, 'w'). I pass it the string I want it to write to the file, and that string contains unicode characters.

完整的错误消息是:

Traceback (most recent call last):
  File "writer.py", line 46, in <module>
    write_listings(c, output_path)
  File "writer.py", line 33, in write_listings
    print name
  File "c:\Python27\lib\encodings\cp437.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 21-26: character maps to <undefined>

这是一个示例字符串: 滑鐵盧安大略加拿大

Here is an example string: 滑鐵盧安大略加拿大

不幸的是,我在创建SSCCE时遇到了麻烦,因为我不能仅仅将字符串文字放入源代码文件中,而不会抱怨我没有声明编码.令人沮丧的是-当我从IDE中运行所有内容时,一切都运行得很好,现在我要去一个unicode兔子洞了!

Unfortunately I'm having trouble creating an SSCCE because I can't just put that string literal into a source code file without it complaining that I haven't declared an encoding. It's frustrating -- this was all working so well when I ran everything from the IDE and now I'm headed down a unicode rabbit hole!

编辑:感谢Fredrik,我现在能够制作SSCCE.在这里:

EDIT: Thanks to Fredrik, I'm now able to make an SSCCE. Here it is:

# -*- coding: utf-8 -*-
str = u'滑鐵盧安大略加拿大'
f = open('test', 'w')
f.write(str)
f.close()

从命令行运行时此SSCCE崩溃,但从IDE中不是运行. 为什么?

This SSCCE crashes when run from command line but not from the IDE. Why is that?

编辑:我添加了Edward Loper建议的一些其他代码,以验证Python版本与命令行版本和IDE版本是否相同.

EDIT: I added some additional code suggested by Edward Loper to verify that the version of Python is identical for the command line and IDE versions.

这是新代码:

# -*- coding: utf-8 -*-
import sys
print sys.version
print open
print open.__module__

str = u'滑鐵盧安大略加拿大'
f = open('test', 'w')
f.write(str)
f.close()

这是从IDE运行时的输出:

Here is the output when run from the IDE:

2.7.3rc2 (default, Mar 18 2012, 22:59:27) [MSC v.1500 64 bit (AMD64)]
<built-in function open>
__builtin__

这是从命令行运行时的输出:

And here is the output when run from the command line:

2.7.3rc2 (default, Mar 18 2012, 22:59:27) [MSC v.1500 64 bit (AMD64)]
<built-in function open>
__builtin__
Traceback (most recent call last):
  File "test.py", line 9, in <module>
    f.write(str)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-8: ordinal not in range(128)

我认为问题仍然没有得到解决,因为我仍然不知道如何使它在IDE中有效,而不是在命令行中有效!

In my opinion, the question is still unanswered because I still have no idea what would make it work in the IDE and not the command line!

推荐答案

正如Fenikso所说,您应该先对字符串进行编码,然后再将其写入文件. file.write()本身不执行此操作的原因是您需要指定要使用的编码(utf-8,utf-16等).有一个python模块编解码器",可让您创建知道要使用哪种编码的流对象,并自动应用它.这就是Fenikso在他的第二个示例中使用的.

As Fenikso said, you should encode a string before writing it to a file. The reason that file.write() doesn't do this itself is that you need to specify which encoding (utf-8, utf-16, etc) you want to use. There's a python module "codecs" which allows you to create stream objects that know what encoding to use, and automatically apply it. That's what Fenikso is using in his second example.

关于您的代码为何可以在IDE中工作而不在命令行中工作的原因,我的猜测是您的IDE会将默认编码"设置为某些非默认值.尝试在IDE和命令行中同时运行它,看看是否有所不同:

As to why your code works in the IDE but not the command line, my guess is that your IDE is setting the "default encoding" to some non-default value. Try running this in both the IDE and the command line and see if it differs:

>>> import sys
>>> print sys.getdefaultencoding()

以下是一些相关信息: http://blog.ianbicking.org/illusive-setdefaultencoding.html

Here's some related information: http://blog.ianbicking.org/illusive-setdefaultencoding.html

这篇关于Python unicode写入文件在命令行中崩溃,但在IDE中不崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆