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
#11
(Sep-16-2018, 11:51 AM)Windspar Wrote: ...
'{0:0>6}\n'
{
0 = first variable
...

That has been redundant in most cases after 2.6 - unless you print a list/tuple and want to use selected elements and/or change the order of elements.
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
#12
Thanks volcano63,

I also try with a for instruction but now an invalid syntax in line 10,

def add_zero(nbr: str, m: int = 6) -> str:
    return "0" * (m - len(nbr)) + nbr
 
with open("dico2.txt","w") as f:
    for a in range(999999):
        if a <= 99999:
            num_str = add_zero(str(a))
        else:
            num_str = str(a)
        f.write(num_str '\n')
        a += 1
Anybody, can this code run just changing syntax in line 10?
Reply
#13
You missed the +

I didn't mean just add '\n'

I meant you need to use concatenation:

string + string

In this case one string comes from a variable the other from a literal string, namely '\n'
I am trying to help you, really, even if it doesn't always seem that way
Reply
#14
Ty gruntfutuk, ty all,

It works now.
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,761 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,602 Apr-05-2023, 07:36 PM
Last Post: Frankduc
Photo Making Zip file of a file and Directory Nasir 2 1,101 Oct-07-2022, 02:01 PM
Last Post: Nasir
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 5,079 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,470 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 8,264 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  When did the number got included in the list? Frankduc 14 3,330 Feb-03-2022, 03:47 PM
Last Post: Frankduc
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,720 Jan-13-2022, 05:22 PM
Last Post: ndc85430
Question Making a copy list in a function RuyCab 1 1,870 Jul-11-2021, 02:06 PM
Last Post: Yoriz
Smile Help making number analysis program Dainer 2 1,846 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