从一个文件写入另一个python [英] Writing from one file to another python

查看:106
本文介绍了从一个文件写入另一个python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从网页获取一些信息,然后将其中的一个变量写入文件,但是我没有运气,这可能很容易,但是我迷路了。

 < div class ='entry qual-5 used-数据质量=5data-australium =normaldata-class =demomandata-particle_effect data-price ='3280000'data-name =Kill-a-Watt Allbrero =56data-paint =data-slot =cosmeticdata-consignment =consignment> 

我在名为data-name的字段之后,它不在每行的同一位置。我尝试过,但没有成功。
$ b $ $ $ $ $ $ $ $ c $ m $打开('itemlist.txt','r')
mfile2 = open('output.txt','a')
用于mfile中的行:
if char =='data-name':
mfile2.write(char)

编辑1:

'hello hi peanut'
if $:

 用于mfile中的行:
print row.index 'hello')

它会按预期输出0,但是当我改变了你好,它没有返回1,它没有返回任何东西。

解决方案

我们试着用普通的字符串操作方法找到这个值:

 >>> line ='''< div class ='entry qual-5 used-demoman slot-head bestprice custom'data-price ='3280000'data-name =Kill-a-Watt Allbrerodata-quality =5 data-australium =normaldata-class =demomandata-particle_effect =56data-paint =data-slot =cosmeticdata-consignment =consignment>'''

我们可以使用 str.index 来查找字符串中字符串的位置:

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

现在我们知道我们需要开始寻找我们感兴趣的属性索引 87

$ p $ >>>数据质量=5数据 - 澳大利亚=正常data-class =demomandata-particle_effect =56数据线[87:]
'数据名=杀死全阿尔贝罗 data-paint =data-slot =cosmeticdata-consignment =consignment>'



<现在,我们还需要删除 data-name =部分:

 >>> start = line.index('data-name')+ len('data-name ='')
>>>开始
98
>>>数据质量=5数据 - 澳大利亚=正常data-class =demomandata-particle_effect =56data-paint = data-slot =cosmeticdata-consignment =consignment>'

现在,我们只需要找到结束引号的索引,然后我们可以提取属性值:

pre code>>> ;> end = line.index('',start)
>>> end
118
>>> line [start:end]
'杀死一个Allbrero'

然后我们有我们的解决方案:

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

我们可以把它放在循环中:

  with open('itemlist.txt', ('data-name')+ len('')作为mfile,打开('output.txt','a')作为mfile2w 
在mfile中的行:
start = line.index data-name =')
end = line.index('',start)
mfile2.write(line [start:end])
mfile2.write('\\\
')


I am trying to take some information I got from a webpage and write one of the variables to a file however I am having no luck it is probably very easy but I'm lost. Here is an example of one of the rows there are 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 am after the field called data-name it is not at the same spot in each row. I tried this but it did not work

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

Edit 1:

I made an example file of 'hello hi peanut' if did:

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

it would print 0 as expected however when I changed the hello to hi it didnt return 1 it returned nothing.

解决方案

Let’s try to find the 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 to find the position of a string within a string:

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

So now we know we need to start looking at index 87 for the attribute we are 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, we need to remove the 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, we just need to find the index of the closing quotation mark too, and then we can extract just the attribute value:

>>> end = line.index('"', start)
>>> end
118
>>> line[start:end]
'Kill-a-Watt Allbrero'

And then we have our solution:

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

We can put that in the loop:

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

这篇关于从一个文件写入另一个python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆