Casting an int to a string in Python
Question:
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
Answers:
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
).
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.
x = 1
y = "foo" + str(x)
Please see the Python documentation: https://docs.python.org/3/library/functions.html#str
You can use str()
to cast it, or formatters:
"ME%d.txt" % (num,)
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()
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
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
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
).
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.
x = 1
y = "foo" + str(x)
Please see the Python documentation: https://docs.python.org/3/library/functions.html#str
You can use str()
to cast it, or formatters:
"ME%d.txt" % (num,)
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()
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