Issue with zero value in string variable (original) (raw)
Hi All
I have an issue with list to Str
Data = message[12:14]
print(Data)
the output is a numeric digit. Example: 04
When i need to show the Data variable in a variable
path = "transaction.log." + date + "_" + **str(Data)** + "*." + str(output1) + "*"
ONLY show me the 0 and not all complete digits about the Data variable get from a list.
example: transaction.log.2025-02-19_0*.sd-a703-937d*
How can I do to show all the values when I assign the Data variable in Path variable?
Regards
jeff5 (Jeff Allen) April 25, 2025, 6:25am 2
Assuming message
is a string, message[12:14]
must be two characters. You should not need to call str()
on it in the expression for path
.
Have you perhaps assigned something to Data
between these statements, like Data = Data[0]
?
I did not properly understand “Data variable get from a list”. If you are storing values of Data
in a list and getting them out, and you get the indexing wrong, it is possible to end up handling individual characters instead of whole strings. Or if you treat Data
as a list at some point, it is treated as a list of its characters.
>>> a = []
>>> b = '007'
>>> a + b
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
a + b
TypeError: can only concatenate list (not "str") to list
>>> a + list(b) # Hmm, so it has to be a list I add?
['0', '0', '7']
>>> a.append(b) # This is the right way
>>> a
['007']
This can happen when you think you are inserting a single object into a list:
>>> a = [0, 1, 2, 3]
>>> a[2:2] = '006' # No. The string is treated as a list (an iterable).
>>> a
[0, 1, '0', '0', '6', 2, 3]
>>> a.insert(1, '005') # This is the right way
>>> a
[0, '005', 1, '0', '0', '6', 2, 3]
CAM-Gerlach (C.A.M. Gerlach) April 27, 2025, 12:45pm 3
Also, quick sidenote—its a lot simpler, more readable and less bug-prone (you have some stray **
s that you probably intend to be strings) to write path
:
as an f-string:
path = f"transaction.log.{date}_**{Data}***.{output1}*"
jeff5 (Jeff Allen) April 29, 2025, 3:54pm 4
I assumed this was meant to be emphasis, which does not work, of course, in formatted blocks..