Python/pyfpdf: TypeError: list indices must be integers or slices, not str

Python/pyfpdf: TypeError: list indices must be integers or slices, not str

我正在尝试传递一个 json 数据并从中制作一个 pdf 文件。 我有这个片段可以正常工作:

data = [
        ['Power:',data["PowerS"],'MVA'],
        ['Voltage:',data["Voltage"],'V'],
        ['Impedance:',data["Impedance"],'ohms'],
        ['Current:',data["CurrentS"],'A'],
    ]

现在我尝试添加另一个与另一个相同的东西:

data2 = [['SC Power',str(data['SC Power']),'MVA'],
    ['Voltage Rating',str(data['Voltage Rating']),'V'],
    ['X/R',str(data['X/RS']),'']
    ]

错误指示列表 data2,我认为它与列表 data 相同。我错过了什么吗?

这是我的代码:

# Import FPDF class
from fpdf import FPDF
import datetime
import json
# Create instance of FPDF class
# Letter size paper, use inches as unit of measure
pdf=FPDF(format='A4', unit='in')
date = datetime.date.today()
file_name = ''
project_Title = ''
class CustomPDF(FPDF):
    # def __init__(self,filename,projectTitle):
    #     self.filename = filename
    #     self.projectTitle = projectTitle
    def header(self):
        # Remember to always put one of these at least once.
        epw = self.w - 2*self.l_margin
        self.set_margins(25,25,25)
        self.add_font('Calibri', '', 'calibri.ttf', uni=True)
        self.add_font('Calibri', 'I', 'calibrii.ttf',uni = True)
        self.add_font('Calibri', 'B', 'calibrib.ttf', uni = True)
        self.set_font('Calibri','B',12.0) 

        self.cell(epw, 5.5, 'Project Title: '+ str(project_Title), ln=1)
        self.cell(epw, 5.5, 'File Name: '+ str(file_name), ln=1)
        self.cell(epw, 5.5, 'Total No. of Bus: 1', ln=1)
        self.cell(epw, 5.5, 'Date: '+str(date), ln=1)
        self.cell(epw, 5.5, 'Page No: ' + str(self.page_no()), ln=1)
        self.line(26, 57, 184, 57)
        self.set_line_width(1)
        # Line break
        self.ln(10)

    def footer(self):
        self.set_y(-32)

        self.set_font('Calibri', '', 12)
        epw = self.w - 2*self.l_margin
        self.line(26, 265, 184, 265)
        self.cell(epw, 5.5, 'SCVD Analyzer 2020', ln=1)

def createPDF(jsn):
    data = json.loads(jsn)
    filename = data["Filename"]
    projectTitle = data["Project Title"]
    global file_name
    global project_Title
    file_name = filename
    project_Title = projectTitle
    pdf = CustomPDF()
    # Create the special value {nb}
    pdf.set_margins(25,25,25)
    pdf.alias_nb_pages()
    pdf.set_auto_page_break(True, 25)
    pdf.add_page()
    pdf.add_font('Calibri', '', 'calibri.ttf', uni=True)
    pdf.add_font('Calibri', 'I', 'calibrii.ttf',uni = True)
    pdf.add_font('Calibri', 'B', 'calibrib.ttf', uni = True)
    epw = pdf.w - 2*pdf.l_margin
    col_width = epw/4
    pdf.set_font('Calibri','B',12.0) 
    pdf.cell(epw, 5.5, "BASE VALUES",ln=1)
    pdf.set_font('Calibri','',12.0)
    data = [
        ['Power:',data["PowerS"],'MVA'],
        ['Voltage:',data["Voltage"],'V'],
        ['Impedance:',data["Impedance"],'ohms'],
        ['Current:',data["CurrentS"],'A'],
    ]
    th = pdf.font_size
    for row in data:
        for datum in row:
            pdf.cell(50, 5.5, str(datum), border=0, align='C')
        pdf.ln(th)

    pdf.set_font('Calibri','B',12.0) 
    pdf.cell(epw, 5.5, "EQUIPMENT PARAMETERS",ln=1)
    pdf.cell(epw, 5.5, "Utility",ln=1)
    data2 = [['SC Power',str(data['SC Power']),'MVA'],
    ['Voltage Rating',str(data['Voltage Rating']),'V'],
    ['X/R',str(data['X/RS']),'']
    ]
    for row in data2:
        for datum in row:
            pdf.cell(50, 5.5, str(datum), border=0, align='C')
        pdf.ln(th)

    pdf.cell(epw, 5.5, "Utility",ln=1)



    pdf.output(filename+".pdf")

if __name__ == '__main__':
    x = {'Filename': 'test111', 
    'Project Title': '123', 
    'PowerS': '100.00', 
    'Voltage': '12000', 
    'Impedance': '1.44', 
    'CurrentS': '8333.3333', 
    'SC Power':'123', 
    'Voltage Rating': '12000', 
    'X/RS': '123123'}

    # convert into JSON:
    y = json.dumps(x)

    # the result is a JSON string:
    print(y)
    createPDF(y)

非常感谢任何帮助。

您的 datalist 对象,无法像 data["PowerS"] 那样访问。您必须像这样使用 整数或切片 作为索引:

>>> data = [
        ['Power:',data["PowerS"],'MVA'],
        ['Voltage:',data["Voltage"],'V'],
        ['Impedance:',data["Impedance"],'ohms'],
        ['Current:',data["CurrentS"],'A'],
    ]
>>> print(data[0][0])
'Power:'
>>> print(data[1][0])
'Voltage:'

如果你想以字符串键的形式访问它,你可以像这样使用 dictionary 对象:

data2 = {
        'Power': [data[0][1],'MVA'],
        'Voltage': [data[1][1],'V'],
        'Impedance': [data[2][1],'ohms'],
        'Current': [data[3][1],'A'],
    }