简体   繁体   English

从文件读取的unicode字符串拆分为unicode字符

[英]Split unicode string read from a file, by a unicode character

I am trying to split a unicode string by a unicode character : '।' 我正在尝试用Unicode字符拆分Unicode字符串:“。” (U+0964) (U + 0964)

An example of the unicode string would be : unicode字符串的示例为:

তার মধ্যে আশ্চর্য একটা মিল। ইস্টার্ন মেট্রোপলিটান বাইপাস। এ বার দিন এল। বদলে গিয়েছে অনেকটাই।

If I read it from a file, 如果我从文件中读取它,

f1 = codecs.open('tmp1.txt',encoding='utf8',mode='r')
data = f1.read()
splitdata = re.split('।',data)
for apart in splitdata :
    print apart

Then the string is not split. 然后,不分割字符串。 Here is the output : 这是输出:

তার মধ্যে আশ্চর্য একটা মিল। ইস্টার্ন মেট্রোপলিটান বাইপাস। এ বার দিন এল। বদলে গিয়েছে অনেকটাই।

However, if I directly place the string into a variable and then split it : 但是,如果我直接将字符串放入变量中,然后将其拆分:

string_var = 'তার মধ্যে আশ্চর্য একটা মিল। ইস্টার্ন মেট্রোপলিটান বাইপাস। এ বার দিন এল। বদলে গিয়েছে অনেকটাই।'
splitstring = re.split('।',string_var)
for apart in splitstring :
    print apart

Then it works. 然后就可以了。 Output : 输出:

তার মধ্যে আশ্চর্য একটা মিল
 ইস্টার্ন মেট্রোপলিটান বাইপাস
 এ বার দিন এল
 বদলে গিয়েছে অনেকটাই

One thing that I have noticed is, if I print the values returned from the split function, I see two different outputs. 我注意到的一件事是,如果我打印从split函数返回的值,则会看到两个不同的输出。

This is the data read from the file split-ed : 这是从split-ed文件读取的数据:

print splitdata
[u'\u09a4\u09be\u09b0 \u09ae\u09a7\u09cd\u09af\u09c7 \u0986\u09b6\u09cd\u099a\u09b0\u09cd\u09af \u098f\u0995\u099f\u09be \u09ae\u09bf\u09b2\u0964 \u0987\u09b8\u09cd\u099f\u09be\u09b0\u09cd\u09a8 \u09ae\u09c7\u099f\u09cd\u09b0\u09cb\u09aa\u09b2\u09bf\u099f\u09be\u09a8 \u09ac\u09be\u0987\u09aa\u09be\u09b8\u0964 \u098f \u09ac\u09be\u09b0 \u09a6\u09bf\u09a8 \u098f\u09b2\u0964 \u09ac\u09a6\u09b2\u09c7 \u0997\u09bf\u09df\u09c7\u099b\u09c7 \u0985\u09a8\u09c7\u0995\u099f\u09be\u0987\u0964\n']

And this is the string split-ed : 这是字符串split-ed:

print splitstring
['\xe0\xa6\xa4\xe0\xa6\xbe\xe0\xa6\xb0 \xe0\xa6\xae\xe0\xa6\xa7\xe0\xa7\x8d\xe0\xa6\xaf\xe0\xa7\x87 \xe0\xa6\x86\xe0\xa6\xb6\xe0\xa7\x8d\xe0\xa6\x9a\xe0\xa6\xb0\xe0\xa7\x8d\xe0\xa6\xaf \xe0\xa6\x8f\xe0\xa6\x95\xe0\xa6\x9f\xe0\xa6\xbe \xe0\xa6\xae\xe0\xa6\xbf\xe0\xa6\xb2',
 ' \xe0\xa6\x87\xe0\xa6\xb8\xe0\xa7\x8d\xe0\xa6\x9f\xe0\xa6\xbe\xe0\xa6\xb0\xe0\xa7\x8d\xe0\xa6\xa8 \xe0\xa6\xae\xe0\xa7\x87\xe0\xa6\x9f\xe0\xa7\x8d\xe0\xa6\xb0\xe0\xa7\x8b\xe0\xa6\xaa\xe0\xa6\xb2\xe0\xa6\xbf\xe0\xa6\x9f\xe0\xa6\xbe\xe0\xa6\xa8 \xe0\xa6\xac\xe0\xa6\xbe\xe0\xa6\x87\xe0\xa6\xaa\xe0\xa6\xbe\xe0\xa6\xb8',
 ' \xe0\xa6\x8f \xe0\xa6\xac\xe0\xa6\xbe\xe0\xa6\xb0 \xe0\xa6\xa6\xe0\xa6\xbf\xe0\xa6\xa8 \xe0\xa6\x8f\xe0\xa6\xb2',
 ' \xe0\xa6\xac\xe0\xa6\xa6\xe0\xa6\xb2\xe0\xa7\x87 \xe0\xa6\x97\xe0\xa6\xbf\xe0\xa7\x9f\xe0\xa7\x87\xe0\xa6\x9b\xe0\xa7\x87 \xe0\xa6\x85\xe0\xa6\xa8\xe0\xa7\x87\xe0\xa6\x95\xe0\xa6\x9f\xe0\xa6\xbe\xe0\xa6\x87',
 '']

It seems that there are two different encodings here, and split is working on the encoding of the string, but does not work with the encoding when the string is read from the file. 似乎这里有两种不同的编码,并且split正在处理字符串的编码,但是当从文件中读取字符串时,split不能用于编码。 Do I need to change the encoding when read from a file to make it work? 从文件读取时是否需要更改编码以使其正常工作?

You need to use 您需要使用

re.split(u'।',data)

Or 要么

data.split(u'।')

u is required since you work with Unicode. u是必需的,因为您使用Unicode。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM