Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Problem processing Chinese character with Python
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Anthony Liu, post: 1753777"] Andrew gave me a sample code with let me read a text file sentence by sentence. Suppose I just wanna read the part between 2 full stops each time. It works nicely with English text files, where the full stop is a dot (.). But when I tried to read Chinese text files, I found that it sometimes reads a few sentences at one time. I guess the reason is that in Chinese, the full stop is not a dot (.), but a little circle, as many of you probably know. Indeed, if I replace the Chinese full stop with the dot. It nicely gets only one sentence each time. So, how should I fix this problem? I am really having headache processing Chinese characters with Python. Here is the sample code that Andrew offered: def bytes(f): # Below: f.read(2) to process Chinese for byte in iter(lambda: f.read(1), ''): yield byte def sentences(iterable): sentence = '' for char in iterable: sentence += char # The little cirlce is the Chinese # full stop. Some of might not be able # view it if you don't have # east Asian language support. if char in ('。','.'): yield sentence.strip() sentence = '' sentence = sentence.strip() if sentence: yield sentence __________________________________ Do you Yahoo!? Yahoo! Search - Find what you’re looking for faster [URL]http://search.yahoo.com[/URL] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Problem processing Chinese character with Python
Top