Python 3.2 - 连接和字符串格式化行为未按预期运行

发布于 2024-11-13 22:11:57 字数 1322 浏览 3 评论 0原文

我想从其他几个变量创建一个“完整文件名”变量,但字符串连接和字符串格式操作的行为不符合我的预期。

我的代码如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生死何惧 2024-11-20 22:11:57

我认为您的示例中使用的方法输入如下所示:

file_date = str(input("Enter file date: "))

可能会在末尾返回一个回车符。
当您尝试打印时,这会导致光标返回到行的开头。
您可能想要修剪 input() 的返回值。

I think the method input used in your example, like so:

file_date = str(input("Enter file date: "))

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().

吻安 2024-11-20 22:11:57

使用这一行来代替换行:

file_date = str(input("Enter file date: ")).rstrip()

Use this line instead to get rid of the line feed:

file_date = str(input("Enter file date: ")).rstrip()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文