What does : TypeError: cannot concatenate str and list objects mean?

2024/10/15 23:25:28

What does this error mean?

TypeError: cannot concatenate 'str' and 'list' objects

Here's part of the code:

for j in ('90.','52.62263.','26.5651.','10.8123.'):if j == '90.':z = ('0.')elif j == '52.62263.':z = ('0.', '72.', '144.', '216.', '288.')for k in z:exepath = os.path.join(exe file location here)exepath = '"' + os.path.normpath(exepath) + '"'cmd = [exepath + '-j' + str(j) + '-n' + str(z)]process=Popen('echo ' + cmd, shell=True, stderr=STDOUT )print process
Answer

I'm not sure you're aware that cmd is a one-element list, and not a string.

Changing that line to the below would construct a string, and the rest of your code will work:

# Just removing the square brackets
cmd = exepath + '-j' + str(j) + '-n' + str(z)

I assume you used brackets just to group the operations. That's not necessary if everything is on one line. If you wanted to break it up over two lines, you should use parentheses, not brackets:

# This returns a one-element list
cmd = [exepath + '-j' + str(j) + '-n' + str(z)]# This returns a string
cmd = (exepath + '-j' + str(j) + '-n' + str(z))

Anything between square brackets in python is always a list. Expressions between parentheses are evaluated as normal, unless there is a comma in the expression, in which case the parentheses act as a tuple constructor:

# This is a string
str = ("I'm a string")# This is a tuple
tup = ("I'm a string","me too")# This is also a (one-element) tuple
tup = ("I'm a string",)
https://en.xdnf.cn/q/69228.html

Related Q&A

How do I create a fixed-length, mutable array of Python objects in Cython?

I need to have an array of python objects to be used in creating a trie datastructure. I need a structure that will be fixed-length like a tuple and mutable like a list. I dont want to use a list bec…

How to install atari-py in Windows 10? [duplicate]

This question already has answers here:OpenAI Gym Atari on Windows(5 answers)Closed 3 years ago.I tried to install lib pack atari-py, and can not find any clear information, most of them wrote that it …

Using pyplot to create grids of plots

I am new to python and having some difficulties with plotting using pyplot. My goal is to plot a grid of plots in-line (%pylab inline) in Juypter Notebook.I programmed a function plot_CV which plots cr…

matplotlib: deliberately block code execution pending a GUI event

Is there some way that I can get matplotlib to block code execution pending a matplotlib.backend_bases.Event?Ive been working on some classes for interactively drawing lines and polygons inside matplo…

Connecting Keras models / replacing input but keeping layers

This questions is similar to Keras replacing input layer. I have a classifier network and an autoencoder network and I want to use the output of the autoencoder (i.e. encoding + decoding, as a preproce…

PySpark 2.x: Programmatically adding Maven JAR Coordinates to Spark

The following is my PySpark startup snippet, which is pretty reliable (Ive been using it a long time). Today I added the two Maven Coordinates shown in the spark.jars.packages option (effectively "…

Python: How to create simple web pages without a huge framework? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

AttributeError: module MySQLdb.constants.FIELD_TYPE has no attribute JSON while migrating in Django

I do not know in what way solve this error. Any hints? I have simple Django projects and receive this error when try to do python3 manage.py migrate. This is related to any programming error in app or…

Downloading file using IE from python

Im trying to download file with Python using IE:from win32com.client import DispatchWithEventsclass EventHandler(object):def OnDownloadBegin(self):passie = DispatchWithEvents("InternetExplorer.App…

Good resources to start python for web development?

Im really interested in learning Python for web development. Can anyone point me in the right direction? Ive been looking at stuff on Google, but havent really found anything that shows proper documen…