如何获取文本中出现的所有单词

【问题】

I have this csv file

Cat, and, dog, bites

Yahoo, news, claims, a, cat, mated, with, a, dog, and, produced, viable, offspring

Cat, killer, likely, is, a, big, dog

Professional, free, advice, on, dog, training, puppy, training

Cat, and, kitten, training, and, behavior

Dog, &, Cat, provides, dog, training, in Eugene, Oregon

Dog, and, cat, is, a, slang, term, used, by, police, officers, for, a, male-female, relationship

Shop, for, your, show, dog, grooming, and, pet, supplies

I want to make all the words start with a small letter and create a list which will include all the unique items from the above csv file. Have you any idea? Thanks in advance! So far, I have managed to convert all the words with a small letter:

unique_row_items = set([field.strip().lower()for field in row])

But i can't manage the other one.

def unique():

    rows = list(csv.reader(open('example_1.csv', 'r'), delimiter=','))

    result = []

    for r in rows:

        key = r

        if key not in result:

            result.append(r)

    return result

Which does not give the results I want

【回答】

这个问题用SPL实现很方便,脚本如下:

A
1= file("D://Words.txt").read()
2=lower(A1)
3=A2.words().id()

A1:从文本读取字符串

A2:将字符串转换成小写字母

A3:通过 words 函数将字符串拆分成单个单词组成的序列,再通过 id 函数去除重复成员

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值