1

I'm trying to write a function that automatically sets up a HTML file using information from a list I've generated. This chunk below is part of a multi-line string.

`"""...
<tr>
  <td>10</td>
  <th>"""+str(List_Data[9])+"""</th>
  <th>"""+str(Extra_Data[9])+"""</th>
</tr>
</table>
..."""`

Extra_Data is a list of number ratings, so Extra_Data[9] = 7.2. When I run the code I get the following error;

`<th>"""+str(Extra_Data[9])+"""</th>
TypeError: can only concatenate str (not "list") to st`

I'm not sure how else I could get this to work since str(Extra_Data[9]) doesn't do the trick and this is the only way I can think to get all of my listed data to be apart of the HTML code efficiently. Any help would be appreciated!

Thanks in advance.

8
  • 1
    This should work. Are you sure you did not accidentally redefine str? Commented Oct 15, 2018 at 8:36
  • Also, did you get that exact error message? Because even if I trigger that sort of error, the wording is different for me, both for Python 2 and 3. I do, however, get a message like that when trying to concatenate a str to list (but not the other way around). And even if str did not do anything, Extra_Data[9] should be float and not list. Commented Oct 15, 2018 at 8:41
  • I'm certain I didn't accidentally redefine str Commented Oct 15, 2018 at 8:42
  • And yes, I did get that exact error message. I agree that it is certainly strange to be getting this error message Commented Oct 15, 2018 at 8:44
  • Then please try to define a minimum example that reproduces that error, including Extra_Data etc. In fact, chances are that doing this, you will find the error yourself. Commented Oct 15, 2018 at 8:46

2 Answers 2

1

Python has the really handy string formatting method .format(), take a look here. All you'd need to do is something along the lines of:

a = """
<tr>
  <td>10</td>
  <th>{0}</th>
  <th>{1}</th>
</tr>
</table>
""".format(List_Data[9], Extra_Data[9])

print a

The numbers in swirly brackets {n} are the placeholders for the argument at the nth index of the .format() function.

To take this one step further, you could iterate over your data lists to create each row programatically:

Table_Array = list()

for index, value in enumerate(List_Data):
    Table_Array.append("""
                       <tr>
                           <td>{0}</td>
                           <th>{1}</th>
                           <th>{2}</th>
                       </tr>
                       """.format(index, List_Data[index], Extra_Data[index])
Sign up to request clarification or add additional context in comments.

6 Comments

While this should work, and is probably cleaner than OP's approach, OP's way should have worked, too, so chances are that the error lies somewhere else.
Agreed, it's an issue that needs more details and a repro to debug.
This doesn't appear to work for my string since it's creating HTML code, and HTML code often features curly brackets around formatting code. When I tried to adapt it, all the formatting gets in the way and makes it unusable for me.
How so? As in it wont be read as text for my HTML formatting? When I run your suggestion I get the following error; """.format(index, List_Data[i], Extra_Data[i])) KeyError: 'font-family'
@B.Stewart I guess you could separate the string that contain inline-CSS and those that use the format method. But anyway, I doubt that this really solves the underlying issue of your code.
|
1

So, in my crazy attempts to solve this issue. I changed the line to this; <th>"""+str(float(Extra_Data[9]))+"""</th> and then it worked. I have no idea why so I changed it back to the original line and then it continued to work. I am so very confused as to why this has happened but as it stands now, my code works and it's the same as it did when it didn't work. I'm not sure why or how.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.