Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9054205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:37:45+00:00 2026-06-16T13:37:45+00:00

here is the html <table> <tr> <td class=break>mono</td> </tr> <tr> <td>c1</td> <td>c2</td> <td>c3</td> </tr>

  • 0

here is the html

<table>
<tr>
<td class="break">mono</td>
</tr>
<tr>
<td>c1</td>
<td>c2</td>
<td>c3</td>
</tr>
<tr>
<td>c11</td>
<td>c22</td>
<td>c33</td>
</tr>
<tr>
<td class="break">dono</td>
</tr>
<tr>
<td>d1</td>
<td>d2</td>
<td>d3</td>
</tr>
<tr>
<td>d11</td>
<td>d22</td>
<td>d33</td>
</tr>
</table>

Now I want output like this in a csv file:

mono c1 c2 c3
mono c11 c22 c33
dono d1 d2 d3
dono d11 d22 d33

But I am getting output like this:

mono
c1 c2 c3
c11 c22 c33
dono
d1 d2 d3
d11 d22 d33

Here is my code:

import codecs
from bs4 import BeautifulSoup
with codecs.open('dump.csv', "w", encoding="utf-8") as csvfile:


    f = open("input.html","r")

    soup = BeautifulSoup(f)
    t = soup.findAll('table')
    for table in t:
        rows = table.findAll('tr')
        for tr in rows:
            cols = tr.findAll('td')
            for td in cols:
                csvfile.write(str(td.find(text=True)))
                csvfile.write(",")
            csvfile.write("\n")

Please help me to resolve this issue.Thanks.

Edit:

Explained with some more details.Here I need to add first section (mono,dono etc) to be appended.

The rule here is that unless I encountered a new “break” class,text inside of that class should be appended to any tr below that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:37:47+00:00Added an answer on June 16, 2026 at 1:37 pm

    Since your new question is effectively an entirely different question from the original, here’s an entirely different answer:

    for table in t:
        rows = table.findAll('tr')
        for row in rows:
            cols = row.findAll('td')
            if 'break' in cols[0].get('class', []):
                header = cols[0].text
            else:
                print header, ' '.join(col.text for col in cols)
    

    I’m assuming that a row will either be exactly 1 “break” column, or 1 or more regular columns. If those assumptions aren’t true, the code can be modified.

    Also, if the generator expression in the join function confuses you, the same thing can be rewritten as an explicit loop: print the header; then for each column, print that column; then print a newline.

    Since you asked for an explanation of 'break' in cols[0].get('class', []), I’ll break it down.

    • cols is a list of the BS4 Tag objects for every td nodes in the current tr node.
    • cols[0] is the first one.
    • cols[0].get('class', []) treats the Tag object as a dictionary, as described in the docs, and calls the familiar get(key, defaultvalue) method on it.
      • In BS4 (unlike older versions), looking up Tag attributes by name always returns a list. While BS3 would return 'foo bar' for <td class='foo bar'> and 'bar' for <td class='foo' class='bar'>, BS4 will return ['foo', 'bar'] for both.
    • Putting it all together, cols[0].get('class', []) will be ['break'] for the <td class='break'> case, and [] for all of the other cases in your sample input.

    As mentioned above, I’m assuming that a row will either be exactly 1 “break” column, or 1 or more regular columns. You can see where I’m making use of those assumptions in the code. But if any of those assumptions are broken, you haven’t told us enough to know what you want to do in those cases.

    If you have any rows with no columns, obviously the cols[0] will raise an IndexError. But you have to decide what to do in that case. Should it do nothing? Print just the header? Change to a state where nothing gets printed until we see a header row? Whatever you decide, it should be easy to code.

    If you have any rows with a header followed by normal rows, the normal rows will be ignored. If you have any headers that aren’t the first column in a row, they will be treated like normal values. If you have multiple headers in the same row, all but the first will be ignored. And so on. In each case, this may or may not be what. But you have to decide what you want, before you can write the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an html table. Here is a simplified version: <table> <tr> <td><div style=display:
http://logging.apache.org/log4net/release/config-examples.html Given the log table here: CREATE TABLE [dbo].[Log] ( [Id] [int] IDENTITY (1,
My html is like this <div id=rsContainer> <table id=rsTable><!-- DATA HERE --></table> </div> and
Consider the following example: ( live demo here ) HTML: <div class=board> <div class=row>
Fiddle is here - http://jsfiddle.net/ashwyn/a45ha/ HTML here - <div class=parent> <div class=a>Class A</div> <div
I have html like so: <div class=foo> (<a href=forum.example.com>forum</a>) <p> Some html here.... </div>
Here is my HTML: <tr> <td colspan=2 class=borderBottomCell> <div class=benefitInfo style=WIDTH: 99%! important;> <asp:DropDownList
Here is my HTML: <div id=leftMenuWrapper> <div id=ramps class=leftMenuHeaderButton></div> <div id=carServiceRamps class=leftMenuSubButton></div> <div class=clear></div>
I want to display a basic html table with controls to toggle showing/hiding of
I have a class that generates some html (form elements and table elements), but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.