Python Forum
Making a number list in a .txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making a number list in a .txt file
#1
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?
Reply
#2
(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.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Thank you for answering me,

I would like to keep my algorithm, how can I change the syntax for causing it works?
Reply
#4
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.
Reply
#5
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
I am trying to help you, really, even if it doesn't always seem that way
Reply
#6
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))
99 percent of computer problems exists between chair and keyboard.
Reply
#7
(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?
I am trying to help you, really, even if it doesn't always seem that way
Reply
#8
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
Reply
#9
+'\n'
I am trying to help you, really, even if it doesn't always seem that way
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,607 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,346 Apr-05-2023, 07:36 PM
Last Post: Frankduc
Photo Making Zip file of a file and Directory Nasir 2 1,064 Oct-07-2022, 02:01 PM
Last Post: Nasir
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,952 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,361 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 8,135 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  When did the number got included in the list? Frankduc 14 3,227 Feb-03-2022, 03:47 PM
Last Post: Frankduc
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,669 Jan-13-2022, 05:22 PM
Last Post: ndc85430
Question Making a copy list in a function RuyCab 1 1,826 Jul-11-2021, 02:06 PM
Last Post: Yoriz
Smile Help making number analysis program Dainer 2 1,794 Jun-24-2021, 09:55 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020