indentation messing up my tuple?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • localpricemaps@gmail.com

    #1

    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.fetchT ext( oRE):
    foodlist.append (oText.strip() + "','")
    food = ''.join(foodlis t)



    for incident in bs('span', {'class' : 'drink'}):
    drink = incident.findNe xtSibling('a', {'class': 'nojs'})
    drinklist = []
    for oText in drink.fetchText ( oRE):
    drinklist.appen d(oText.strip() + "','")
    drink = ''.join(drinkli st)


    tuple = (food + drink + "\n")
    data.append(tup le)
    f = open("test.txt" , 'a')
    f.write ( ''.join( tuple ) )

  • Steven D'Aprano

    #2
    Re: indentation messing up my tuple?

    On Fri, 27 Jan 2006 16:45:54 -0800, localpricemaps wrote:
    [color=blue]
    > here is my code:
    >
    > for row in bs('div', {'style' : 'both'}):[/color]

    What is bs? Is it a secret?

    [color=blue]
    > data=[]
    >
    > for incident in bs('h3', {'class' : 'name'}):
    > foodlist = []
    > for oText in incident.fetchT ext( oRE):
    > foodlist.append (oText.strip() + "','")
    > food = ''.join(foodlis t)[/color]

    Put a "print food" statement at the end of this line to see what you have.
    [color=blue]
    > for incident in bs('span', {'class' : 'drink'}):
    > drink = incident.findNe xtSibling('a', {'class': 'nojs'}) drinklist =
    > []
    > for oText in drink.fetchText ( oRE):
    > drinklist.appen d(oText.strip() + "','")
    > drink = ''.join(drinkli st)[/color]

    Put a "print drink" statement at the end of this line to see what you have.
    [color=blue]
    > tuple = (food + drink + "\n")
    > data.append(tup le)[/color]

    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?
    [color=blue]
    > f = open("test.txt" , 'a')
    > f.write ( ''.join( tuple ) )[/color]

    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(da ta)
    f.close()


    --
    Steven.

    Comment

    • bonono@gmail.com

      #3
      Re: indentation messing up my tuple?


      Steven D'Aprano wrote:[color=blue]
      > On Fri, 27 Jan 2006 16:45:54 -0800, localpricemaps wrote:
      >[color=green]
      > > here is my code:
      > >
      > > for row in bs('div', {'style' : 'both'}):[/color]
      >
      > What is bs? Is it a secret?[/color]

      seems to be beautiful soup, in this context.

      Comment

      • localpricemaps@gmail.com

        #4
        Re: indentation messing up my tuple?

        sorry i forgot to add in the code for my tuple which is at the very end

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

        Comment

        • localpricemaps@gmail.com

          #5
          Re: indentation messing up my tuple?

          sorry i left out my tuple which is at the end of my code

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

          Comment

          • infidel

            #6
            Re: indentation messing up my tuple?

            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(foo d + drink)
            f.write('\n'.jo in(data))

            Comment

            • localpricemaps@gmail.com

              #7
              Re: indentation messing up my tuple?

              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:[color=blue]
              > 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(foo d + drink)
              > f.write('\n'.jo in(data))[/color]

              Comment

              • infidel

                #8
                Re: indentation messing up my tuple?

                > i am using a tuple because i am building lists.

                I don't understand
                [color=blue]
                > 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)[/color]

                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:
                [color=blue][color=green]
                > > (food + drink + '\n') is not a tuple, (food + drink + '\n',) is[/color][/color]


                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.

                Comment

                • localpricemaps@gmail.com

                  #9
                  Re: indentation messing up my tuple?

                  the, the issue is that the last loop adds the last value of everything
                  to the data array

                  Comment

                  Working...