Я работаю над фрагментом кода, который преобразует HTML-таблицы в CSV-файл. У меня проблемы с выяснением, как убрать пробел между информацией, которую я печатаю на терминал, используя разделение строк. Мой лучший результат - терминал печатает большие промежутки между информацией, что затрудняет навигацию. Любая информация будет глубоко цениться.
import csv
from bs4 import BeautifulSoup
from termcolor import cprint
html = open("recallist.html").read()
soup = BeautifulSoup(html)
table = soup.find_all('div', {'id': 'PrintArea'})
output_rows = []
recals = 'recallist.csv'
cprint('READING TABLES', 'green')
for table_row in table:
columns = table_row.findAll('td')
output_row = []
for column in columns:
output_row.append(column.text)
output_rows.append(output_row)
with open('recallist.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(output_rows)
with open(recals, 'r') as f:
contents = f.read()
for item in contents.split("Date,Customer,Phone,Cell Phone,Removal,Notes"):
for refine in item.split('",,'):
print(refine)
Образец CSV, указанный ниже:
Location,,,Date,Customer,Phone,Cell Phone,Removal,Notes,�,�,�,,04/29/19 | 03:00 PM,[9999] FIRST LAST,999-999-9999***,999-999-9999,,"
",,"
","
$127.92
",,04/29/19 | 03:30 PM,[123456] FIRST LAST,999-999-9999***,999-999-9999,04/13/2020,"
",,"
","
$0.02
",,04/29/19 | 04:00 PM,[123456] FIRST LAST,999-999-9999***,,09/10/2019,"
",,"
","
($212.10)
",,04/29/19 | 04:15 PM,[123456] FIRST LAST,999-999-9999***,,01/09/2020,"
",,"
","
$16.23
",,04/29/19 | 04:30 PM,[123456] FIRST LAST,999-999-9999***,,05/30/2019,"
",,"
","
$0.24
",,04/29/19 | 05:00 PM,[123456] FIRST LAST,999-999-9999***,,07/26/2019,"
",,"
","
($0.30)
",,04/29/19 | 07:00 PM,[123456] FIRST LAST,999-999-9999***,999-999-9999,11/15/2019,"
",,"
","
$0.06
",,04/29/19 | 07:30 PM,[123456] FIRST LAST,999-999-9999***,,12/12/2019,"
",,"
","
Формат, который я пытаюсь достичь:
04/29/19 | 03:00 PM,[9999] FIRST LAST,999-999-9999***,999-999-9999,$127.92
04/29/19 | 03:30 PM,[99999] FIRST LAST,999-999-9999***,999-999-9999,$0.02
ETC.
Образец HTML, если он необходим:
<tbody><tr class="alt">
<td colspan="5" align="left" style="background-color:668cd9;">Location</td>
<td colspan="5" align="left" style="background-color:668cd9;"></td>
</tr>
<tr align="left" class="GrayBLOCK">
<td></td>
<td>Date</td>
<td>Customer</td>
<td>Phone</td>
<td>Cell Phone</td>
<td>Removal</td>
<td>Notes</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr class="alt">
<td></td>
<td>04/29/19 | 03:00 PM</td>
<td><a href="../code/c_newClient.cfm?theID=99999" target="_blank">[9999]</a> FIRST LAST</td>
<td>999-999-9999***</td>
<td>999-999-9999</td>
<td></td>
<td>
</td>
<td></td>
<td>
</td>
<td align="right" class="RedMED">
$127.92
</td>
</tr>
<tr>
<td></td>
<td>04/29/19 | 03:30 PM</td>
<td><a href="../code/c_newClient.cfm?theID=99999" target="_blank">[999999]</a> FIRST LAST</td>
<td>999-999-9999***</td>
<td>999-999-9999</td>
<td>04/13/2020</td>
<td>
</td>
<td></td>
<td>
</td>
<td align="right" class="RedMED">
$0.02
</td>
</tr>
<tr class="alt">
<td></td>
<td>04/29/19 | 04:00 PM</td>
<td><a href="../code/c_newClient.cfm?theID=99999" target="_blank">[999999]</a> FIRST LAST</td>
<td>999-999-9999***</td>
<td></td>
<td>09/10/2019</td>
<td>
</td>
<td></td>
<td>
</td>
<td align="right" class="RedMED">
($212.10)
</td>
</tr>
<tr>
<td></td>
<td>04/29/19 | 04:15 PM</td>
<td><a href="../code/c_newClient.cfm?theID=99999" target="_blank">[999999]</a> FIRST LAST</td>
<td>999-999-9999***</td>
<td></td>
<td>01/09/2020</td>
<td>
</td>
<td></td>
<td>
</td>
<td align="right" class="RedMED">
$16.23
</td>
</tr>
<tr class="alt">
<td></td>
<td>04/29/19 | 04:30 PM</td>
<td><a href="../code/c_newClient.cfm?theID=99999" target="_blank">[999999]</a> FIRST LAST</td>
<td>999-999-9999***</td>
<td></td>
<td>05/30/2019</td>
<td>
</td>
<td></td>
<td>
</td>
<td align="right" class="RedMED">
$0.24
</td>
</tr>
<tr>
<td></td>
<td>04/29/19 | 05:00 PM</td>
<td><a href="../code/c_newClient.cfm?theID=99999" target="_blank">[999999]</a> FIRST LAST</td>
<td>999-999-9999***</td>
<td></td>
<td>07/26/2019</td>
<td>
</td>
<td></td>
<td>
</td>
<td align="right" class="RedMED">
($0.30)
</td>
</tr>
<tr class="alt">
<td></td>
<td>04/29/19 | 07:00 PM</td>
<td><a href="../code/c_newClient.cfm?theID=99999" target="_blank">[999999]</a> FIRST LAST</td>
<td>999-999-9999***</td>
<td>999-999-9999</td>
<td>11/15/2019</td>
<td>
</td>
<td></td>
<td>
</td>
<td align="right" class="RedMED">
$0.06
</td>
</tr>