python标点符号分中英文嘛,简洁的标点符号并分成单词python

学习python目前还有一点问题.我试图从另一个子程序中取一条线并将其转换为单独的单词,除了少数几个之外,它们已经被删除了标点符号.该程序的输出应该是它显示的单词和行号.应该是这样的 – >字:[1]

输入文件:

please. let! this3 work.

I: hope. it works

and don't shut up

码:

def createWordList(line):

wordList2 =[]

wordList1 = line.split()

cleanWord = ""

for word in wordList1:

if word != " ":

for char in word:

if char in '!,.?":;0123456789':

char = ""

cleanWord += char

print(cleanWord," cleaned")

wordList2.append(cleanWord)

return wordList2

输出:

anddon't:[3]

anddon'tshut:[3]

anddon'tshutup:[3]

ihope:[2]

ihopeit:[2]

ihopeitworks:[2]

pleaselet:[1]

pleaseletthis3:[1]

pleaseletthis3work:[1]

我不确定这是由什么引起的,但我学会了Ada并在短时间内转换到python.

解决方法:

您应该将cleanWord设置回外部循环的每次迭代顶部的空字符串:

def createWordList(line):

wordList2 =[]

wordList1 = line.split()

for word in wordList1:

cleanWord = ""

for char in word:

if char in '!,.?":;0123456789':

char = ""

cleanWord += char

wordList2.append(cleanWord)

return wordList2

请注意,我还删除了if word!=“”,因为在line.split()之后你将永远不会有空格.

>>> createWordList('please. let! this3 work.')

['please', 'let', 'this', 'work']

标签:strip,punctuation,word,python,line

来源: https://codeday.me/bug/20190725/1536939.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值