小编Dav*_*d A的帖子

将字符串拆分为单词和标点符号

我正在尝试将字符串拆分为单词和标点符号,将标点符号添加到拆分生成的列表中.

例如:

>>> c = "help, me"
>>> print c.split()
['help,', 'me']
Run Code Online (Sandbox Code Playgroud)

我真正希望列表看起来像是:

['help', ',', 'me']
Run Code Online (Sandbox Code Playgroud)

所以,我希望字符串在空格中分割,并且从单词中分割出标点符号.

我试图先解析字符串,然后运行拆分:

>>> for character in c:
...     if character in ".,;!?":
...             outputCharacter = " %s" % character
...     else:
...             outputCharacter = character
...     separatedPunctuation += outputCharacter
>>> print separatedPunctuation
help , me
>>> print separatedPunctuation.split()
['help', ',', 'me']
Run Code Online (Sandbox Code Playgroud)

这会产生我想要的结果,但在大文件上却很慢.

有没有办法更有效地做到这一点?

python string split

57
推荐指数
5
解决办法
6万
查看次数

wxPython - DatePickerCtrl 似乎忽略了 SetValue()

我正在尝试使用以下代码预先填充 wxPython DatePicker 的值:

month, day, year = runData[2][0:8].split('/')
displayDate = wx.DateTimeFromDMY(int(day), int(month) - 1, int(year))
self.datePicker.SetValue(displayDate)
Run Code Online (Sandbox Code Playgroud)

以下是打印值:

  • 运行数据 [2] = 12/16/09 00:00
  • 月、日、年 = 12 16 09
  • 显示日期 = 12/16/09 00:00:00

但是,datePicker 总是显示今天的日期。

有任何想法吗?

我在 Windows 7 上使用 Python 2.6.4 和 wxPython 2.8.10.1。

谢谢。


20/12/09 16:30 更新:

当我尝试处理来自有问题的 DatePickerCtrl 的信息时,出现以下错误:

Traceback (most recent call last):
  File "test.py", line 1120, in onOk
    dateLong = self.datePicker.GetValue()
  File "c:\python26\lib\site-packages\wx-2.8-msw-unicode\wx\_controls.py", line
6465, in GetValue
    return _controls_.DatePickerCtrlBase_GetValue(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "m_date.IsValid() == dt.IsValid() …
Run Code Online (Sandbox Code Playgroud)

python wxpython

5
推荐指数
1
解决办法
2585
查看次数

标签 统计

python ×2

split ×1

string ×1

wxpython ×1