473,603 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with lists and writing to file in correct order

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"><t d class="tall"><a name="D0L1" "href="his/food"
target="_blank" >

<td class="desc"><h 2 id="foodName">p izza</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>

Dec 26 '05 #1
17 1388
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.

Dec 27 '05 #2
sorry guys, here is the code

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

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

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

Dec 27 '05 #3
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.fetchT ext( oRE):
price += oText.strip() + "','"

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

for incident in bs('h2', {'id' : 'food'}):
food = ""
for oText in incident.fetchT ext( 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.

Dec 27 '05 #4
here is the write part:

out = open("test.txt" , 'a')
out.write (store+ food+ price + "\n")
out.close()
Steven D'Aprano wrote:
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.fetchT ext( oRE):
price += oText.strip() + "','"

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

for incident in bs('h2', {'id' : 'food'}):
food = ""
for oText in incident.fetchT ext( 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.


Dec 27 '05 #5
the problem with writing to teh file immidiately is that it ends up
writing all food items together, and then all store items and then all
prices

i want

food, store, price
food, store, price

Dec 27 '05 #6
ho***********@g mail.com wrote:
the problem with writing to teh file immidiately is that it ends up
writing all food items together, and then all store items and then all
prices

i want

food, store, price
food, store, price

Well, if it all fits in memory, append each to its own list, and then
either finally if you can or periodically if you must:

for food, store, price in zip(foods, stores, prices):
<do some writing.>

--
-Scott David Daniels
sc***********@a cm.org
Dec 27 '05 #7
sorry for asking such beginner questions but i tried this and nothing
wrote to my text file

for food, price, store in bs(food, price, store):
out = open("test.txt" , 'a')
out.write (food + price + store)
out.close()
while if i write the following without the for i at least get
something?
out = open("test.txt" , 'a')
out.write (food + price + store)
out.close()
Scott David Daniels wrote:
ho***********@g mail.com wrote:
the problem with writing to teh file immidiately is that it ends up
writing all food items together, and then all store items and then all
prices

i want

food, store, price
food, store, price

Well, if it all fits in memory, append each to its own list, and then
either finally if you can or periodically if you must:

for food, store, price in zip(foods, stores, prices):
<do some writing.>

--
-Scott David Daniels
sc***********@a cm.org


Dec 27 '05 #8

homepricem...@g mail.com wrote:
sorry for asking such beginner questions but i tried this and nothing
wrote to my text file

for food, price, store in bs(food, price, store):
out = open("test.txt" , 'a')
out.write (food + price + store)
out.close()
while if i write the following without the for i at least get
something?
out = open("test.txt" , 'a')
out.write (food + price + store)
out.close()

pull the open() and close() call out of the loop. And use some other
name for the variables as they are very confusing and could be error
prone to.

Dec 27 '05 #9
On Mon, 26 Dec 2005 20:56:17 -0800, homepricemaps wrote:
sorry for asking such beginner questions but i tried this and nothing
wrote to my text file

for food, price, store in bs(food, price, store):
out = open("test.txt" , 'a')
out.write (food + price + store)
out.close()
What are the contents of food, price and store? If "nothing wrote to my
text file", chances are all three of them are the empty string.

while if i write the following without the for i at least get
something?
out = open("test.txt" , 'a')
out.write (food + price + store)
out.close()


You get "something" . That's not much help. But I predict that what you are
getting is the contents of food price and store, at least one of which are
not empty.

You need to encapsulate your code by separating the part of the code that
reads the html file from the part that writes the text file. I suggest
something like this:
def read_html_data( name_of_file):
# I don't know BeautifulSoup, so you will have to fix this...
datafile = BeautifulSoup(n ame_of_file)
# somehow read in the foods, prices and stores
# for each set of three, store them in a tuple (food, store, price)
# then store the tuples in a list
# something vaguely like this:
data = []
while 1:
food = datafile.get("f ood") # or whatever
store = datafile.get("s tore")
price = datafile.get("p rice")
data.append( (food,store,pri ce) )
datafile.close( )
return data

def write_data_to_t ext(datalist, name_of_file):
# Expects a list of tuples (food,store,pri ce). Writes that list
# to name_of_file separated by newlines.
fp = file(name_of_fi le, "w")
for triplet in datalist:
fp.write("Food = %s, store = %s, price = %s\n" % triplet
fp.close()
Hope this helps.

--
Steven.

Dec 27 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4287
by: Mike | last post by:
How do I extract a list of lists from a user defined function and print the results as strings for each list?
10
3267
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project. To implement my image format of choice I need to work with big-endian bytes, where 'byte' of course means '8 bits', not 'sizeof(char)'. It seems that I could use bitset<8> to represent a byte in my code --- if you have a better suggestion, I welcome it --- but that still leaves me with the question of how to write those bitsets to an image file as big-endian bytes...
11
1981
by: The_Kingpin | last post by:
Hi all, I'm new to C programming and looking for some help. I have a homework project to do and could use every tips, advises, code sample and references I can get. Here's what I need to do. I have a file named books.txt that contains all the informations on the books. Each book is a struc containing 6 fields written on separated line in the
6
4976
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
1
3700
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
0
5546
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
51
8609
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort for linked lists, I couldn't find one. I read something about Mcilroy's "Optimistic Merge Sort" and studied some implementation, but they were for arrays. Does anybody know if Mcilroys optimization is applicable to truly linked lists at all?
11
1760
by: rshepard | last post by:
I start with a list of tuples retrieved from a database table. These tuples are extracted and put into individual lists. So I have lists that look like this: . When I concatenate lists, I end up with a list of lists that looks like this: . ]. Then, I average the column values so I end up with a single list, but with two brackets on each end, for example, ]. Unfortunately, when I try to use that last list in a NumPy function, I'm told...
1
2925
by: vikjohn | last post by:
I have a new perl script sent to me which is a revision of the one I am currently running. The permissions are the same on each, the paths are correct but I am getting the infamous : The specified CGI application misbehaved by not returning a complete set of HTTP headers. The scripts are very long but here are the opening statements: The One that works .... #!C:\Perl\bin\perl.exe # openresolver.cgi # # OpenResolver - a CGI script for...
0
7996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7928
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8415
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8060
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3903
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3951
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.