Python 3.2 - 连接和字符串格式化行为未按预期运行
我想从其他几个变量创建一个“完整文件名”变量,但字符串连接和字符串格式操作的行为不符合我的预期。
我的代码如下:
file_date = str(input("Enter file date: "))
root_folder = "\\\\SERVER\\FOLDER\\"
file_prefix = "sample_file_"
file_extension = ".txt"
print("")
print("Full file name with concatenation: ")
print(root_folder + file_prefix + file_date + file_extension)
print("Full file name with concatenation, without file_extension: ")
print(root_folder + file_prefix + file_date)
print("")
print("")
print("Full file name with string formatting: ")
print("%s%s%s%s" % (root_folder, file_prefix, file_date, file_extension))
print("Full file name with string formatting, without file_extension: ")
print("%s%s%s" % (root_folder, file_prefix, file_date))
print("")
运行脚本时的输出是:
C:\Temp>python test.py
Enter file date: QT1
Full file name with concatenation:
.txtRVER\FOLDER\sample_file_QT1
Full file name with concatenation, without file_extension:
\\SERVER\FOLDER\sample_file_QT1
Full file name with string formatting:
.txtRVER\FOLDER\sample_file_QT1
Full file name with string formatting, without file_extension:
\\SERVER\FOLDER\sample_file_QT1
我期望它在最后连接“.txt”,只是它用它替换了字符串的前四个字符。
如何将扩展变量连接到字符串的末尾,而不是让它替换字符串的前 n 个字符?
除了如何解决这个特定问题之外,我还想知道为什么我首先遇到它。我做错了什么/我不知道 Python 3.2 的哪些行为?
I want to create a "full file name" variable from several other variables, but the string concatenation and string format operations aren't behaving the way I expect.
My code is below:
file_date = str(input("Enter file date: "))
root_folder = "\\\\SERVER\\FOLDER\\"
file_prefix = "sample_file_"
file_extension = ".txt"
print("")
print("Full file name with concatenation: ")
print(root_folder + file_prefix + file_date + file_extension)
print("Full file name with concatenation, without file_extension: ")
print(root_folder + file_prefix + file_date)
print("")
print("")
print("Full file name with string formatting: ")
print("%s%s%s%s" % (root_folder, file_prefix, file_date, file_extension))
print("Full file name with string formatting, without file_extension: ")
print("%s%s%s" % (root_folder, file_prefix, file_date))
print("")
The output when I run the script is:
C:\Temp>python test.py
Enter file date: QT1
Full file name with concatenation:
.txtRVER\FOLDER\sample_file_QT1
Full file name with concatenation, without file_extension:
\\SERVER\FOLDER\sample_file_QT1
Full file name with string formatting:
.txtRVER\FOLDER\sample_file_QT1
Full file name with string formatting, without file_extension:
\\SERVER\FOLDER\sample_file_QT1
I was expecting it to concatenate the ".txt" at the very end, except it's replacing the first four characters of the string with it instead.
How do I concatenate the extension variable to the end of the string instead of having it replace the first n characters of the string?
In addition to how to solve this particular problem, I'd like to know why I ran into it in the first place. What did I do wrong/what Python 3.2 behavior am I not aware of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的示例中使用的方法输入如下所示:
可能会在末尾返回一个回车符。
当您尝试打印时,这会导致光标返回到行的开头。
您可能想要修剪 input() 的返回值。
I think the method input used in your example, like so:
may be returning a carriage return character at the end.
This causes the cursor to go back to the start of the line when you try to print it out.
You may want to trim the return value of input().
使用这一行来代替换行:
Use this line instead to get rid of the line feed: