0

I'm trying to build a table from user input to export via Cheetah to fill a template to use as a report. I'm having trouble separating each iteration of the loop "for j in range(1, numErrors):" and put table row tags at the beginning and end of each concatenation.

table = ""
cells = ""
row = ""

numMeas = int(raw_input("Enter total number of measurements: "))
numMeas = numMeas + 1 #number of measurements compensated for iteration behavior
for i in range(1, numMeas):
    typeMeas = raw_input("Enter type of measurement "+str(i)+": ")
    numErrors = int(raw_input("Enter number of error sources: "))
    numErrors = numErrors + 1
    for j in range(1, numErrors): #builds dataSet from number of errors
        inputData = []
        inputData.append(typeMeas)
        description = raw_input("Enter source of uncertainty "+str(j)+": ")
        inputData.append(description)
        estUncert = raw_input("Enter estimated uncertainty "+str(j)+": ")
        estUncert = float(estUncert)
        inputData.append(str(estUncert))

        for i in inputData:
            cell = "<td>"+str(i)+"</td>"
            cells += cell

    table = "<tr>"+cells+"</tr>"+"\n"

print table

Current output:
<tr><td>mass</td><td>scale</td><td>1.0</td><td>mass</td><td>human</td><td>2.0</td>               <td>temp</td><td>room</td><td>3.0</td><td>temp</td><td>therm</td><td>4.0</td></tr>

Desired output:
<tr><td>mass</td><td>scale</td><td>1.0</td></tr>
<tr><td>mass</td><td>human</td><td>2.0</td></tr>
<tr><td>temp</td><td>room</td><td>3.0</td></tr>
<tr><td>temp</td><td>therm</td><td>4.0</td></tr>
4

1 回答 1

1

我猜它可能需要看起来像这样:

table = ""
cells = ""
row = ""

numMeas = int(raw_input("Enter total number of measurements: "))
numMeas = numMeas + 1 #number of measurements compensated for iteration behavior
for i in range(1, numMeas):
    typeMeas = raw_input("Enter type of measurement "+str(i)+": ")
    numErrors = int(raw_input("Enter number of error sources: "))
    numErrors = numErrors + 1
    inputData = []
    for j in range(1, numErrors): #builds dataSet from number of errors
        inputData.append(typeMeas)
        description = raw_input("Enter source of uncertainty "+str(j)+": ")
        inputData.append(description)
        estUncert = raw_input("Enter estimated uncertainty "+str(j)+": ")
        estUncert = float(estUncert)
        inputData.append(str(estUncert))
    cells = ''
    for i in inputData:
        cell = "<td>"+str(i)+"</td>"
        cells += cell

    table += "<tr>"+cells+"</tr>"+"\n"

print table
于 2013-06-18T22:43:26.090 回答