1
votes

Hi I have a csv file in the form of

Col1  Col2  Col3  Col4 col5
AA    BA     CA    DA   EA
AB    BB     CB    DB   EB
AC    BC     CC    DC   EC
AD    BD     CD    DD   ED

How do I get around to create a new csv file with a new column COLTAble New csv will be in the format. Col1 Col2 Coltable Where coltable uses col3,col4, and col5 and takes the form of

<table border="1" cellpadding="0" cellspacing="0">
<tbody>
    <tr>
        <td rowspan="2" style="width:50%;">
        COL3
        </td>
        <td style="width:50%;">
        COL4
        </td>
    </tr>
    <tr>
        <td style="width:50%;">
        COL5
        </td>
    </tr>
</tbody>

1
I want a third column to contain html , with information from col3,Col4and col5.BKCapri
What trouble are you having creating html and putting it into a csv file?Peter Wood
Its the other way round. Im trying to create an HTML column by merging 3 columns into an HTML table. Hence I will still end up with a CSV file.BKCapri

1 Answers

1
votes

I'm not sure I fully understand your question. Does your final csv have a different html table string for every row?

Nonetheless, pandas is really useful for reading and writing files like this.

>>> import pandas as pd
>>> df = pd.read_csv('temp.csv')
>>> print df
     Col1 Col2 Col3 Col4 col5
0   AA   BA   CA   DA   EA
1   AB   BB   CB   DB   EB
2   AC   BC   CC   DC   EC
3   AD   BD   CD   DD   ED



>>> df['Coltable'] = df.apply(lambda row: row[2:].to_frame().to_html(), axis=1)
>>> df_new = df.iloc[:, [0, 1, -1]]
>>> print df_new
Col1 Col2                                           Coltable
0   AA   BA  <table border="1" class="dataframe">\n  <thead...
1   AB   BB  <table border="1" class="dataframe">\n  <thead...
2   AC   BC  <table border="1" class="dataframe">\n  <thead...
3   AD   BD  <table border="1" class="dataframe">\n  <thead...
    df_new.to_csv('temp_new.csv', index=False)


>>> df_new.to_csv('temp_new.csv', index=False)

Now temp_new.csv looks like

Col1,Col2,Coltable
AA,BA,"<table border=""1"" class=""dataframe"">
  <thead>
    <tr style=""text-align: right;"">
      <th></th>
      <th>0</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Col3</th>
      <td>CA</td>
    </tr>
    <tr>
      <th>Col4</th>
      <td>DA</td>
    </tr>
    <tr>
      <th>col5</th>
      <td>EA</td>
    </tr>
  </tbody>
</table>"
AB,BB,"<table border=""1"" class=""dataframe"">
  <thead>
    <tr style=""text-align: right;"">
      <th></th>
      <th>1</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Col3</th>
      <td>CB</td>
    </tr>
    <tr>
      <th>Col4</th>
      <td>DB</td>
    </tr>
    <tr>
      <th>col5</th>
      <td>EB</td>
    </tr>
  </tbody>
</table>"
AC,BC,"<table border=""1"" class=""dataframe"">
  <thead>
    <tr style=""text-align: right;"">
      <th></th>
      <th>2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Col3</th>
      <td>CC</td>
    </tr>
    <tr>
      <th>Col4</th>
      <td>DC</td>
    </tr>
    <tr>
      <th>col5</th>
      <td>EC</td>
    </tr>
  </tbody>
</table>"
AD,BD,"<table border=""1"" class=""dataframe"">
  <thead>
    <tr style=""text-align: right;"">
      <th></th>
      <th>3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Col3</th>
      <td>CD</td>
    </tr>
    <tr>
      <th>Col4</th>
      <td>DD</td>
    </tr>
    <tr>
      <th>col5</th>
      <td>ED</td>
    </tr>
  </tbody>
</table>"