帮助列表并以正确的顺序写入文件 [英] help with lists and writing to file in correct order

查看:63
本文介绍了帮助列表并以正确的顺序写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,


有一个逻辑问题。提前感谢帮助。


我正在从html中抓取3条信息,即食物

名称,商店名称和价格。我正在为许多不同的食物项目发现这个html包括比萨饼,汉堡包,薯条等什么

i想要写出一个文本文件以下订单:


披萨,披萨小屋,3.00

汉堡包,汉堡王,4.00

面条,熊猫店,2.00


html如下。有没有人有一个很好的建议如何设置

代码以这样的方式写入文件文件

之前列出?我做过的任何尝试似乎都写到了文件

这样的


面条,熊猫客栈,3

面条,熊猫客栈,4

面条,熊猫客栈,2

HTML

< tr class =" base">< td class = tall>< a name =" D0L1" " href =" his / food"

target =" _blank">


< td class =" desc">< ; h2 id =" foodName"> pizza< / h2>


target =" _blank">< b> pizza hutt< / b>< / a>< / div>


< td class =" price"> 3.00< / td>

< tr>

解决方案

2005年12月26日星期一13:54:37 -0800,homepricemaps写道:

嘿伙计,
<你有一个逻辑问题。提前感谢帮助。

我正在从html中抓取3条信息,即食品名称,商店名称和价格。我正在为包括披萨,汉堡,薯条等html中的许多不同的食物项目这样做。我想要的是按以下顺序写出文本文件:

比萨饼,披萨小屋,3.00汉堡,汉堡王,4.00
面条,熊猫客栈,2.00

html如下。有没有人有一个很好的建议,如何设置
代码以这种方式写入以前列出的文本文件?我做过的任何尝试似乎都写到了这样的文件

面条,熊猫客栈,3个面条,熊猫客栈,4个面条,熊猫客栈, 2




如果您发布代码,而不是发布HTML?除非我们看到您的代码,否则您希望我们如何找到它中的错误?


-

史蒂文。


对不起伙计们,这是bs中的事件代码


('''',{ ''class'':''price''}):

price =""

for fact.fetchText(oRE)中的oText:

price + = oText.strip()+"'',''"


for bs事件(''div'',{''class' ':''store''}):

store =""

for fact.fetchText(oRE)中的oText:

store + = oText.strip()+"'',''"


for bs事件(''h2'',{''id'':'' food''}):

food =""

for event.fetchText(oRE)中的oText:

food + = oText .strip()+"'',''"


2005年12月26日星期一17:44:43 -0800,homepricemaps wro te:

对不起家伙,这里是bs事件的代码

(''''',{''class'':''价格''}):
price =""
对于events.fetchText(oRE)中的oText:
价格+ = oText.strip()+"'',''" ;

bs事件(''div'',{''class'':''store''}):
store =""
for event.fetchText(oRE)中的oText:
存储+ = oText.strip()+"'',''"

用于事件bs(''h2'', {''id'':''food''}):
food =""
for incident.fetchText(oRE)中的oText:
food + = oText.strip( )+"'',''"



这几乎不是你所有的代码 - 你实际上在哪里?
*写*东西到文件?问题是你正在一遍又一遍地向文件中写入相同的商品和食物。在您收集了一行商店/食品后,您必须立即将其写入文件,或者在

中将其保存在列表中以便您可以编写该批次最后。

-

史蒂文。


hey folks,

have a logic question for you. appreciate the help in advance.

i am scraping 3 pieces of information from the html namely the food
name , store name and price. and i am doing this for many different
food items found ni the html including pizza, burgers, fries etc. what
i want is to write out to a text file in the following order:

pizza, pizza hut, 3.00
burgers, burger king, 4.00
noodles, panda inn, 2.00

html is below. does anyone have good recommendation for how to setup
the code in such a manner where it writes to the text file in th order
listed previously? any attempt i have made seems to write to the file
like this

noodles, panda inn, 3
noodles, panda inn, 4
noodles, panda inn, 2
HTML
<tr class="base"><td class="tall"><a name="D0L1" "href="his/food"
target="_blank">

<td class="desc"><h2 id="foodName">pizza</h2>

<div class="store"><a name="D0L3" "href="/xPopups/nojs"
target="_blank"><b>pizza hutt</b></a></div>

<td class="price">3.00</td>
<tr>

解决方案

On Mon, 26 Dec 2005 13:54:37 -0800, homepricemaps wrote:

hey folks,

have a logic question for you. appreciate the help in advance.

i am scraping 3 pieces of information from the html namely the food
name , store name and price. and i am doing this for many different
food items found ni the html including pizza, burgers, fries etc. what
i want is to write out to a text file in the following order:

pizza, pizza hut, 3.00
burgers, burger king, 4.00
noodles, panda inn, 2.00

html is below. does anyone have good recommendation for how to setup
the code in such a manner where it writes to the text file in th order
listed previously? any attempt i have made seems to write to the file
like this

noodles, panda inn, 3
noodles, panda inn, 4
noodles, panda inn, 2



Instead of posting the HTML, how about if you post your code? Unless we
see your code, how do you expect us to find the bug in it?

--
Steven.


sorry guys, here is the code

for incident in bs(''a'', {''class'' : ''price''}):
price = ""
for oText in incident.fetchText( oRE):
price += oText.strip() + "'',''"

for incident in bs(''div'', {''class'' : ''store''}):
store = ""
for oText in incident.fetchText( oRE):
store += oText.strip() + "'',''"

for incident in bs(''h2'', {''id'' : ''food''}):
food = ""
for oText in incident.fetchText( oRE):
food += oText.strip() + "'',''"


On Mon, 26 Dec 2005 17:44:43 -0800, homepricemaps wrote:

sorry guys, here is the code

for incident in bs(''a'', {''class'' : ''price''}):
price = ""
for oText in incident.fetchText( oRE):
price += oText.strip() + "'',''"

for incident in bs(''div'', {''class'' : ''store''}):
store = ""
for oText in incident.fetchText( oRE):
store += oText.strip() + "'',''"

for incident in bs(''h2'', {''id'' : ''food''}):
food = ""
for oText in incident.fetchText( oRE):
food += oText.strip() + "'',''"


This is hardly all your code -- where is the part where you actually
*write* something to the file? The problem is you are writing the same
store and food to the file over and over again. After you have collected
one line of store/food, you must write it to the file immediately, or at
least save it in a list so you can write the lot at the end.
--
Steven.


这篇关于帮助列表并以正确的顺序写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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