在 Python 中将 int 转换为字符串

发布于 2024-09-27 19:51:35 字数 323 浏览 3 评论 0原文

我希望能够生成多个名称为 fileX.txt 的文本文件,其中 X 是某个整数:

for i in range(key):
    filename = "ME" + i + ".txt" //Error here! Can't concat a string and int
    filenum = filename
    filenum = open(filename , 'w')  

是否有其他人知道如何执行 filename = "ME" + i 部分,以便我获得具有这些名称的文件列表:“ME0.txt”、“ME1.txt”、“ME2.txt”等

I want to be able to generate a number of text files with the names fileX.txt where X is some integer:

for i in range(key):
    filename = "ME" + i + ".txt" //Error here! Can't concat a string and int
    filenum = filename
    filenum = open(filename , 'w')  

Does anyone else know how to do the filename = "ME" + i part so I get a list of files with the names: "ME0.txt" , "ME1.txt" , "ME2.txt" , and etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

抹茶夏天i‖ 2024-10-04 19:51:36

您可以使用 str() 来转换它,或格式化程序:

"ME%d.txt" % (num,)

You can use str() to cast it, or formatters:

"ME%d.txt" % (num,)
呆° 2024-10-04 19:51:36

这是您的整个代码的答案:

key =10

files = ("ME%i.txt" % i for i in range(key))

#opening
files = [ open(filename, 'w') for filename in files]

# processing
for i, file in zip(range(key),files):
    file.write(str(i))
# closing
for openfile in files:
    openfile.close()

Here answer for your code as whole:

key =10

files = ("ME%i.txt" % i for i in range(key))

#opening
files = [ open(filename, 'w') for filename in files]

# processing
for i, file in zip(range(key),files):
    file.write(str(i))
# closing
for openfile in files:
    openfile.close()
蓝眼睛不忧郁 2024-10-04 19:51:36

适用于 Python 3 和 Python 2.7

for i in range(50):
    filename = "example{:03d}.txt".format(i)
    print(filename)

使用格式 {:03d} 获得 3 位数字并以 0 开头,这是一个很好的技巧,可以让文件在资源管理器中查看时以正确的顺序显示。将括号留空 {} 以实现默认格式。

# output
example000.txt
example001.txt
example002.txt
example003.txt

Works on Python 3 and Python 2.7

for i in range(50):
    filename = "example{:03d}.txt".format(i)
    print(filename)

Use format {:03d} to have 3 digits with leading 0, it's a great trick to have the files appear in the right order when looking at them in the explorer. Leave brackets empty {} for default formatting.

# output
example000.txt
example001.txt
example002.txt
example003.txt
水中月 2024-10-04 19:51:35
x = 1
y = "foo" + str(x)

请参阅Python文档: https://docs.python.org/3/库/functions.html#str

x = 1
y = "foo" + str(x)

Please see the Python documentation: https://docs.python.org/3/library/functions.html#str

白色秋天 2024-10-04 19:51:35

对于 2.6 之前的 Python 版本,请使用 字符串格式化运算符 %

filename = "ME%d.txt" % i

对于 2.6 及更高版本,请使用 str .format() 方法:

filename = "ME{0}.txt".format(i)

虽然第一个示例在 2.6 中仍然有效,但第二个示例是首选。

如果您有超过 10 个文件以这种方式命名,您可能需要添加前导零,以便文件在目录列表中正确排序:

filename = "ME%02d.txt" % i
filename = "ME{0:02d}.txt".format(i)

这将生成类似 ME00.txt的文件名ME99.txt。如需更多数字,请将示例中的 2 替换为更大的数字(例如 ME{0:03d}.txt)。

For Python versions prior to 2.6, use the string formatting operator %:

filename = "ME%d.txt" % i

For 2.6 and later, use the str.format() method:

filename = "ME{0}.txt".format(i)

Though the first example still works in 2.6, the second one is preferred.

If you have more than 10 files to name this way, you might want to add leading zeros so that the files are ordered correctly in directory listings:

filename = "ME%02d.txt" % i
filename = "ME{0:02d}.txt".format(i)

This will produce file names like ME00.txt to ME99.txt. For more digits, replace the 2 in the examples with a higher number (eg, ME{0:03d}.txt).

吹泡泡o 2024-10-04 19:51:35

要么:

"ME" + str(i)

要么:

"ME%d" % i

通常首选第二个,特别是当您想从多个标记构建字符串时。

Either:

"ME" + str(i)

Or:

"ME%d" % i

The second one is usually preferred, especially if you want to build a string from several tokens.

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