Python从查询中创建嵌套的html表

时间:2018-05-08 14:16:58

标签: python python-3.6

我有以下数据:

[{"Name": "CREATIVE OIL & GAS OPERATING,LLC", "District": "1", "County": 
"GONZALES (TX)", "OFS Region": "GULF COAST", "Lease Name": "ALFORD"}, 
{"Name": "KALER ENERGY CORP.", "District": "2", "County": "JACKSON (TX)", 
"OFS Region": "GULF COAST", "Lease Name": "DUGGER UNIT"}, {"Name": "ENCANA 
OIL & GAS(USA) INC.", "District": "2", "County": "PECOS (TX)", "OFS 
Region": "PERMIAN BASIN", "Lease Name": "HONS UNIT"}]

我正在尝试将数据转换为这种格式:

<h3>OFS Region: GULF COAST</h3>
<H4>County: Gonzales</H4>

<table style="width:30%">
  <tr>
    <th>Company</th>
    <th>Lease Name</th>
  </tr>
  <tr>
    <td>CREATIVE OIL & GAS OPERATING,LLC</td>
    <td>ALFORD</td>
  </tr>
</table>

<H4>County: Jackson</H4>

<table style="width:30%">
  <tr>
    <th>Company</th>
    <th>Lease Name</th>
  </tr>
  <tr>
    <td>KALER ENERGY CORP.</td>
    <td>DUGGER UNIT</td>
  </tr>
</table>


<h3>OFS Region: Permian Basin</h3>
<H4>County: Pecos</H4>

<table style="width:30%">
  <tr>
    <th>Company</th>
    <th>Lease Name</th>
  </tr>
  <tr>
    <td>ENCANA OIL & GAS(USA) INC.</td>
    <td>HONS UNIT</td>

  </tr>

所以基本上我试图弄清楚如何在某些列('Name','Lease Name')上动态创建表格,然后将其与其他几列('OFS Region','District')分开排序,'县')它给了我地狱。我已经尝试将数据放在json,defaultdict,nested_dict,pandas pivot_table和其他几种方式中。

以下是我尝试过的一些例子:

rows = cur.fetchall()
d = defaultdict(defaultdict)
for row in rows:
    ofs_region = row[3]
    district = row[1]
    county = row[2]
    name = row[0]
    lease_name = row[4]

    a = {'Name': name, 'Lease Name': lease_name}

    d[ofs_region][district][county] = a

另一个例子:

b = {}
for row in rows:
    ofs_region = row[3]
    district = row[1]
    county = row[2]
    name = row[0]
    lease_name = row[4]

    list = [name, lease_name]

    if ofs_region not in b:
        b[ofs_region] = ofs_region

    if district not in b[ofs_region]:
        b[ofs_region][district]  = district
        b[ofs_region] = ({'district': {district: {'name': name, 'lease name': lease_name}}})

    if district not in a[ofs_region]:
        a[ofs_region][district] = district

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以创建一个类来存储数据,然后使用str.join格式化表格:

data = [{"Name": "CREATIVE OIL & GAS OPERATING,LLC", "District": "1", "County": "GONZALES (TX)", "OFS Region": "GULF COAST", "Lease Name": "ALFORD"}, {"Name": "KALER ENERGY CORP.", "District": "2", "County": "JACKSON (TX)", "OFS Region": "GULF COAST", "Lease Name": "DUGGER UNIT"}, {"Name": "ENCANA OIL & GAS(USA) INC.", "District": "2", "County": "PECOS (TX)", "OFS Region": "PERMIAN BASIN", "Lease Name": "HONS UNIT"}]
class Company:
  def __init__(self, d):
    self.__dict__ = {a.replace(' ', '_').lower():b for a, b in d.items()}

companies = [Company(i) for i in data]
block = """
<h3>OFS Region: {}</h3>
<H4>County: {}</H4>

<table style="width:30%">
 <tr>
  <th>Company</th>
  <th>Lease Name</th>
  </tr>
  <tr>
  <td>{}</td>
 <td>{}</td>
 </tr>
</table>
""" 
final_result = '\n\n'.join(block.format(i.ofs_region, i.county, i.name, i.lease_name) for i in companies)

输出:

<h3>OFS Region: GULF COAST</h3>
<H4>County: GONZALES (TX)</H4>

<table style="width:30%">
<tr>
 <th>Company</th>
 <th>Lease Name</th>
 </tr>
 <tr>
 <td>CREATIVE OIL & GAS OPERATING,LLC</td>
<td>ALFORD</td>
 </tr>
 </table>



<h3>OFS Region: GULF COAST</h3>
<H4>County: JACKSON (TX)</H4>

<table style="width:30%">
<tr>
<th>Company</th>
 <th>Lease Name</th>
 </tr>
 <tr>
 <td>KALER ENERGY CORP.</td>
 <td>DUGGER UNIT</td>
 </tr>
</table>



 <h3>OFS Region: PERMIAN BASIN</h3>
 <H4>County: PECOS (TX)</H4>

<table style="width:30%">
<tr>
 <th>Company</th>
 <th>Lease Name</th>
   </tr>
   <tr>
 <td>ENCANA OIL & GAS(USA) INC.</td>
 <td>HONS UNIT</td>
  </tr>
</table>