形式我不知道我完全理解你的問題。你的最終csv每行都有不同的html表格字符串嗎?
儘管如此,熊貓對於閱讀和編寫這樣的文件非常有用。
>>> 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)
現在temp_new.csv看起來像
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>"
我想第三列包含HTML,與COL3,Col4and COL5信息。 – BKCapri
創建html並將其放入csv文件中有什麼困難? –
其他方式。我試圖通過將3列合併到一個HTML表中來創建一個HTML列。因此,我仍然會得到一個CSV文件。 – BKCapri