Writing from one file to another python -


i trying take information got webpage , write 1 of variables file having no luck easy i'm lost. here example of 1 of rows there 1253 rows.

<div class='entry qual-5 used-demoman slot-head bestprice custom' data-price='3280000' data-name="kill-a-watt allbrero" data-quality="5" data-australium="normal" data-class="demoman" data-particle_effect="56" data-paint="" data-slot="cosmetic" data-consignment="consignment"> 

i after field called data-name not @ same spot in each row. tried did not work

mfile=open('itemlist.txt','r') mfile2=open('output.txt','a') row in mfile:     if char =='data-name':         mfile2.write(char) 

edit 1:

i made example file of 'hello hi peanut' if did:

for row in mfile:     print row.index('hello') 

it print 0 expected when changed hello hi didnt return 1 returned nothing.

let’s try find value using common string manipulation methods:

>>> line = '''<div class='entry qual-5 used-demoman slot-head bestprice custom' data-price='3280000' data-name="kill-a-watt allbrero" data-quality="5" data-australium="normal" data-class="demoman" data-particle_effect="56" data-paint="" data-slot="cosmetic" data-consignment="consignment">''' 

we can use str.index find position of string within string:

>>> line.index('data-name') 87 

so know need start looking @ index 87 attribute interested in:

>>> line[87:] 'data-name="kill-a-watt allbrero" data-quality="5" data-australium="normal" data-class="demoman" data-particle_effect="56" data-paint="" data-slot="cosmetic" data-consignment="consignment">' 

now, need remove data-name=" part too:

>>> start = line.index('data-name') + len('data-name="') >>> start 98 >>> line[start:] 'kill-a-watt allbrero" data-quality="5" data-australium="normal" data-class="demoman" data-particle_effect="56" data-paint="" data-slot="cosmetic" data-consignment="consignment">' 

now, need find index of closing quotation mark too, , can extract attribute value:

>>> end = line.index('"', start) >>> end 118 >>> line[start:end] 'kill-a-watt allbrero' 

and have our solution:

start = line.index('data-name') + len('data-name="') end = line.index('"', start) print(line[start:end]) 

we can put in loop:

with open('itemlist.txt','r') mfile, open('output.txt','a') mfile2w     line in mfile:         start = line.index('data-name') + len('data-name="')         end = line.index('"', start)         mfile2.write(line[start:end])         mfile2.write('\n') 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -