473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

indentation messing up my tuple?

i have the following code which is used to create a tuple of food and
drink. if the page i am trying to scrape has a total of 10 food/drink
items that i end up getting a nice list of 10 food/drink items in my
text file BUT they are all a repeat of the first item so i end up
getting a text file that looks like this:

shrimp, coke
shrimp, coke
shrimp, coke

instead of being

shrimp, coke
hamburger, oj

here is my code:

for row in bs('div', {'style' : 'both'}):
data=[]

for incident in bs('h3', {'class' : 'name'}):
foodlist = []
for oText in incident.fetchText( oRE):
foodlist.append(oText.strip() + "','")
food = ''.join(foodlist)

for incident in bs('span', {'class' : 'drink'}):
drink = incident.findNextSibling('a', {'class': 'nojs'})
drinklist = []
for oText in drink.fetchText( oRE):
drinklist.append(oText.strip() + "','")
drink = ''.join(drinklist)
tuple = (food + drink + "\n")
data.append(tuple)
f = open("test.txt", 'a')
f.write ( ''.join( tuple ) )

Jan 28 '06 #1
8 1946
On Fri, 27 Jan 2006 16:45:54 -0800, localpricemaps wrote:
here is my code:

for row in bs('div', {'style' : 'both'}):
What is bs? Is it a secret?

data=[]

for incident in bs('h3', {'class' : 'name'}):
foodlist = []
for oText in incident.fetchText( oRE):
foodlist.append(oText.strip() + "','")
food = ''.join(foodlist)
Put a "print food" statement at the end of this line to see what you have.
for incident in bs('span', {'class' : 'drink'}):
drink = incident.findNextSibling('a', {'class': 'nojs'}) drinklist =
[]
for oText in drink.fetchText( oRE):
drinklist.append(oText.strip() + "','")
drink = ''.join(drinklist)
Put a "print drink" statement at the end of this line to see what you have.
tuple = (food + drink + "\n")
data.append(tuple)
This seems awfully pointless. At the beginning of every loop, you set data
to the empty list. After a while you append one tuple to it. But you don't
use data again, and it just gets reset to the empty list at the beginning
of the next loop.

What is the purpose of data?
f = open("test.txt", 'a')
f.write ( ''.join( tuple ) )


You are re-opening the file every single time around the loop. You should
either do this:

f = open("test.txt", "w")
for row in bs(...):
# processing
f.write(one line only)
f.close()

or do this:

data = []
for row in bs(...):
# processing
# accumulate everything you want in data
f.writelines(data)
f.close()
--
Steven.

Jan 28 '06 #2

Steven D'Aprano wrote:
On Fri, 27 Jan 2006 16:45:54 -0800, localpricemaps wrote:
here is my code:

for row in bs('div', {'style' : 'both'}):


What is bs? Is it a secret?


seems to be beautiful soup, in this context.

Jan 28 '06 #3
sorry i forgot to add in the code for my tuple which is at the very end

tuple = (food+ drink + "\n")
data.append(tuple)
f = open("froogle.sql", 'a')
f.write ( ''.join( tuple )

Jan 28 '06 #4
sorry i left out my tuple which is at the end of my code

tuple = (food + drink + "\n")
data.append(tuple)
f = open("froogle.sql", 'a')
f.write ( ''.join( tuple )

Jan 28 '06 #5
tuple is the name of the built-in type, so it's not a very good idea to
reassign it to something else.

(food + drink + '\n') is not a tuple, (food + drink + '\n',) is

There's no reason to use tuples here, just do this:

data.append(food + drink)
f.write('\n'.join(data))

Jan 31 '06 #6
i am using a tuple because i am building lists. if i just use (food +
drink) then while drink is unique food remains the same do i get this:

(burger, coke)
(burger, 7up)
(burger, sprite)

infidel wrote:
tuple is the name of the built-in type, so it's not a very good idea to
reassign it to something else.

(food + drink + '\n') is not a tuple, (food + drink + '\n',) is

There's no reason to use tuples here, just do this:

data.append(food + drink)
f.write('\n'.join(data))


Feb 1 '06 #7
> i am using a tuple because i am building lists.

I don't understand
if i just use (food +
drink) then while drink is unique food remains the same do i get this:

(burger, coke)
(burger, 7up)
(burger, sprite)


I don't understand what you're saying here.
food and drink are both strings. adding them together gives you a new
string. putting parentheses around a string does not give you a tuple,
you need that magic comma to get python to recognize the expression as
a tuple.

As I said:
(food + drink + '\n') is not a tuple, (food + drink + '\n',) is

And since all you're doing with the data list is joining into a single
string, I still don't see where you need any tuples.

Feb 1 '06 #8
the, the issue is that the last loop adds the last value of everything
to the data array

Feb 4 '06 #9

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

Similar topics

15
by: Hilbert | last post by:
Hello, I'm using python to output RIB streams for Renderman. The RIB stream is a bunch of statements which describes a 3d image. The Rib standard allows for blocks which we usually indent for...
177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
7
by: diffuser78 | last post by:
I am a newbie to Python. I am mainly using Eric as the IDE for coding. Also, using VIM and gedit sometimes. I had this wierd problem of indentation. My code was 100% right but it wont run...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
2
by: Alan Isaac | last post by:
I am probably confused about immutable types. But for now my questions boil down to these two: - what does ``tuple.__init__`` do? - what is the signature of ``tuple.__init__``? These...
4
by: Alex Vinokur | last post by:
Here is some tuple (triple in this case) with uniform types (for instance, double): boost::tuple<double, double, doublet; Is there any way (in boost::tuple) to define such tuples something like...
0
by: Hatem Nassrat | last post by:
on Wed Jun 13 10:17:24 CEST 2007, Diez B. Roggisch deets at nospam.web.de wrote: Well I have looked into this and it seems that using the list comprehension is faster, which is...
1
by: Eric S. Johansson | last post by:
in trying to make programming in Python more accessible to disabled programmers (specifically mobility impaired speech recognition users), and hitting a bit of a wall. The wall (for today) is...
19
by: Eric S. Johansson | last post by:
Almar Klein wrote: there's nothing like self interest to drive one's initiative. :-) 14 years with speech recognition and counting. I'm so looking to my 15th anniversary of being injured next...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.