Python Forum

Full Version: Making a number list in a .txt file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,

I would like making a list of number in a file I can read with notepad.

I try this:

f=open("dico.txt","w")
a=0
while a<=999999:
	if a<=99999:
		def add_zero(nbr: str, m: int = 6) -> str:
			return "0" * (m - len(nbr)) + nbr
	f.write(str(a))
	a=a+1
	else:
    f.write(str(a))
	a=a+1
f.close()
A syntax error in the "else" line appears.

Could you tell me why?
(Sep-15-2018, 08:59 PM)kwak86 Wrote: [ -> ]
f=open("dico.txt","w")
a=0
while a<=999999:
	if a<=99999:
		def add_zero(nbr: str, m: int = 6) -> str:
			return "0" * (m - len(nbr)) + nbr
	f.write(str(a))
	a=a+1
	else:
    f.write(str(a))
	a=a+1
f.close()
A syntax error in the "else" line appears.

Could you tell me why?

Because your indents are off - but as I understand, you are trying to write 0-padded values to a file, so you define a function for it (in a very inefficient way, I must add) and then you never call it.

The proper - and much easier way would be to use formatting - if you number is called number - one-letter names is a bad practice - f'{number:06d} will do the trick in Python versions 3.6 and above. Up to 3.5, '{:06d}'.format(number) will do.
Thank you for answering me,

I would like to keep my algorithm, how can I change the syntax for causing it works?
I try to change my indents decreasing the place of else, but the same error still appears.

What is a padded value please, I find nothing about it on Google.
pad means add specified characters to fill string to required width before/after the primary content.

I've amended your code to:
  • use a with statement to open the file and automatically close it
  • moved function that pads to outside of main code, and called it
  • corrected the structure, and removed repetition

Regarding the better way of doing this, look up in the official python documentation string formatting, the format method and f-strings.

You might as well use a for loop as you know how many times you are writing data out, rather than a while loop and have to increment the counter.

def add_zero(nbr: str, m: int = 6) -> str:
    return "0" * (m - len(nbr)) + nbr

with open("dico.txt","w") as f:
    a = 0
    while a <= 999999:
        if a <= 99999:
            num_str = add_zero(str(a))
        else:
            num_str = str(a)
        f.write(num_str)
        a += 1
with open("dico.txt","w") as f:
    for a in range(999999):
        f.write('{0:0>6}\n'.format(a))

'{0:0>6}\n'
{
0 = first variable
:0 = fill
> = left adjust
6 = six spaces
}

left adjust not needed for zero padding
with open("dico.txt","w") as f:
    for a in range(999999):
        f.write('{0:06}\n'.format(a))
(Sep-16-2018, 11:51 AM)Windspar Wrote: [ -> ]
with open("dico.txt","w") as f:
    for a in range(999999):
        f.write('{0:0>6}\n'.format(a))

'{0:0>6}\n'
{
0 = first variable
:0 = fill
> = left adjust
6 = six spaces
}

left adjust not needed for zero padding
with open("dico.txt","w") as f:
    for a in range(999999):
        f.write('{0:06}\n'.format(a))

I'd intended to leave it to the OP to figure out how to do the padding without the function they originally coded.

Not sure the OP wants the \n between numbers either.

That said, why not f-strings rather than format?
Hello,

Thanks for your precious help,

This code seems ok excepted I cannot reach the line by adding a '\n' expression. As soon as I add a ' behind the variable in the f.write(num_str \n)line, my editor tells me num_str is not a variable.

def add_zero(nbr: str, m: int = 6) -> str:
    return "0" * (m - len(nbr)) + nbr
 
with open("dico.txt","w") as f:
    a = 0
    while a <= 999999:
        if a <= 99999:
            num_str = add_zero(str(a))
        else:
            num_str = str(a)
        f.write(num_str)
        a += 1
+'\n'
Thanks gruntfutuk,

CMD returns me an invalid syntax with the following code, neitherless no more changes in my editor

def add_zero(nbr: str, m: int = 6) -> str:
    return "0" * (m - len(nbr)) + nbr
 
with open("dico.txt","w") as f:
    a = 0
    while a <= 999999:
        if a <= 99999:
            num_str = add_zero(str(a))
        else:
            num_str = str(a)
        f.write(num_str'\n')
        a += 1
Pages: 1 2