尝试使用Python将字符串拆分为多个部分

时间:2012-11-18 02:36:51

标签: python unicode split

我正在尝试以下列方式拆分字符串。这是一个示例字符串:

"Hello this is a string.-2.34 This is an example1 string."

请注意,“”是U + F8FF unicode字符,字符串的类型是Unicode。

我想将字符串分解为:

"Hello this is a string.","-2.34"," This is an example1 string."

我写了一个正则表达式来分割字符串但是使用它我无法得到我想要的数字部分。 (第一个字符串中的-2.34)

我的代码:

import re
import os
from django.utils.encoding import smart_str, smart_unicode

text = open(r"C:\data.txt").read()
text = text.decode('utf-8')
print(smart_str(text))

pat = re.compile(u"\uf8ff-*\d+\.*\d+")
newpart = pat.split(text)
firstpart = newpart[::1]

print ("first part of the string ----")
for f in firstpart:
f = smart_str(f)
print ("-----")
print f

1 个答案:

答案 0 :(得分:5)

如果要将括号保留在-*\d+\.*\d+的结果中,则需要在re.split附近加上括号:

import re
text = u"Hello this is a string.\uf8ff-2.34 This is an example1 string."
print(re.split(u'\uf8ff(-*\d+\.*\d+)', text))

产量

[u'Hello this is a string.', u'-2.34', u' This is an example1 string.']