在一组文件中查找非ascii字符 [英] Finding non ascii characters in a set of files

查看:109
本文介绍了在一组文件中查找非ascii字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在将我的程序更新为Python 2.5,但我一直遇到

编码问题。在我的脚本的任何一个开头都没有定义生态。我想要做的是扫描目录并列出其中包含非ascii字符的所有

文件。我怎么会这样做?
这样做?


谢谢,


巴里。

Hi,

I''m updating my program to Python 2.5, but I keep running into
encoding problems. I have no ecodings defined at the start of any of
my scripts. What I''d like to do is scan a directory and list all the
files in it that contain a non ascii character. How would I go about
doing this?

Thanks,

Barry.

推荐答案

2月23日下午2:38,b ... @ yahoo.com写道:
On Feb 23, 2:38 pm, b...@yahoo.com wrote:




我正在将我的程序更新为Python 2.5,但我一直遇到

编码问题。在我的脚本的任何一个开头都没有定义生态。我想要做的是扫描目录并列出其中包含非ascii字符的所有

文件。我怎么去这个呢?

这样做?
Hi,

I''m updating my program to Python 2.5, but I keep running into
encoding problems. I have no ecodings defined at the start of any of
my scripts. What I''d like to do is scan a directory and list all the
files in it that contain a non ascii character. How would I go about
doing this?



这样的事情怎么样:

content = open( ''file.py'')。read()

试试:

content.encode(''ascii'')

除了UnicodeDecodeError :

print" file.py包含非ascii字符"

How about something like this:
content = open(''file.py'').read()
try:
content.encode(''ascii'')
except UnicodeDecodeError:
print "file.py contains non-ascii characters"


2月24日凌晨2:12,Peter Bengtsson < pete ... @ gmail.comwrote:
On Feb 24, 2:12 am, "Peter Bengtsson" <pete...@gmail.comwrote:

2月23日下午2:38,b ... @ yahoo.com写道:
On Feb 23, 2:38 pm, b...@yahoo.com wrote:


Hi,


我正在将我的程序更新为Python 2.5,但我一直在遇到

编码问题。在我的脚本的任何一个开头都没有定义生态。我想要做的是扫描目录并列出其中包含非ascii字符的所有

文件。我怎么会这样做?
这样做?
I''m updating my program to Python 2.5, but I keep running into
encoding problems. I have no ecodings defined at the start of any of
my scripts. What I''d like to do is scan a directory and list all the
files in it that contain a non ascii character. How would I go about
doing this?



这样的事情怎么样:

content = open(''file.py'')。read()

尝试:

content.encode(''ascii'')

除了UnicodeDecodeError:

print" file.py包含非ascii字符


How about something like this:
content = open(''file.py'').read()
try:
content.encode(''ascii'')
except UnicodeDecodeError:
print "file.py contains non-ascii characters"



Peter Bengtsson写道:
Peter Bengtsson wrote:

2月23日,2:38 pm,b ... @ yahoo.com写道:
On Feb 23, 2:38 pm, b...@yahoo.com wrote:

>

我正在将我的程序更新为Python 2.5 ,但我一直遇到
编码问题。在我的脚本的任何一个开头都没有定义生态。我想要做的是扫描目录并列出其中包含非ascii字符的所有
文件。我该怎么做呢?
>Hi,

I''m updating my program to Python 2.5, but I keep running into
encoding problems. I have no ecodings defined at the start of any of
my scripts. What I''d like to do is scan a directory and list all the
files in it that contain a non ascii character. How would I go about
doing this?



这样的事情怎么样:

content = open(''file。 py'')。read()

试试:

content.encode(''ascii'')

除了UnicodeDecodeError:

print" file.py包含非ascii字符"


How about something like this:
content = open(''file.py'').read()
try:
content.encode(''ascii'')
except UnicodeDecodeError:
print "file.py contains non-ascii characters"



下一个问题是非文本文件将包含非-ASCII

个字符(字节)。另一个''问题'是OP没有说文件有多大
,所以.read()可能是个问题。


-Larry

The next problem will be that non-text files will contain non-ASCII
characters (bytes). The other ''issue'' is that OP didn''t say how large
the files were, so .read() might be a problem.

-Larry


这篇关于在一组文件中查找非ascii字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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