动态创建文件名时不理解这个字符串连接错误

Don't understand this string concatenation error when dynamically making a filename

我有一个奇怪的字符串连接错误,我似乎无法理解。

这是我的代码:

message = clientSocket.recv( 8000 ).decode( "UTF-8" )
data=message.split(",")#split the string using delimiter
ID=data.pop()#remove and return ID
ID=''.join(ID)#make the list into a string
ID=ID.replace("\n", "")#strip the \n
Pdata=data
print(ID)
print(type(ID))
print(Pdata)
#Dynamically make new filename using time and ID
filename = datetime.now().strftime("%Y%md_%H%M%S%f")+"."+ID+".csv"
print(filename)

这里的输出是:

H3
<class 'str'>
['+2.07767994E-06', '+8.7037......']
.csv0314_185741582956.H3

前几个字符似乎被 ".csv" 覆盖了。我正在尝试获取正确的文件名,它应该是 '20190314_185741582956.H3.csv'.

我无法正确打开文件名并在其中存储 Pdata,出现该错误。 我错过了什么吗?

如果变量 ID 包含一个 \r 字符,那么您可能会得到您显示的输出。添加另一个替换以将“\r”替换为“”,您将获得更好看(且有效)的文件名