[Tutor] Regular expression
Steven D'Aprano
steve at pearwood.info
Tue Sep 23 13:02:07 CEST 2014
On Tue, Sep 23, 2014 at 11:40:25AM +0200, jarod_v6 at libero.it wrote:
> Hi there!!
>
> I need to read this file:
>
> pippo.count :
> 10566 ZXDC
> 2900 ZYG11A
> 7909 ZYG11B
> 3584 ZYX
> 9614 ZZEF1
> 17704 ZZZ3
> How can extract only the number and the work in array? Thanks for any help
There is no need for the nuclear-powered bulldozer of regular
expressions just to crack this peanut.
with open('pippo.count') as f:
for line in f:
num, word = line.split()
num = int(num)
print num, word
Or, if you prefer the old-fashioned way:
f = open('pippo.count')
for line in f:
num, word = line.split()
num = int(num)
print num, word
f.close()
but the first way with the with-statement is better.
--
Steven
More information about the Tutor
mailing list