0

I was to be able to create a file using python which has data in the specific format shown below. It starts from 00000# where # is 1 and goes all the way to 100000. The same number is present in each line before and after the text My Team.

#output file
000001 My Team 000001
000002 My Team 000002
...all the way to
100000 My Team 100000

I am not sure on how to do this format creation in python. I know it is something simple but I am a bit lost on how to proceed. I am very new to python and I would appreciate if anyone can give me some guidance on this.

Thank you to all.

4 Answers 4

1

You can try f-strings added in python 3.6:

PADDING = 6

with open('test.txt', 'w') as fh:
    for i in range(1, 100001):
        print(f'{i:0{PADDING}d} My Team {i:0{PADDING}d}', file=fh)

OUTPUT:

000001 My Team 000001
000002 My Team 000002
...
100000 My Team 100000

Another method would be to use str.zfill (zfill stands from zero-fill):

>>> i = 1
>>> f'{str(i).zfill(PADDING)} My Team {str(i).zfill(PADDING)}'
000001 My Team 000001
Sign up to request clarification or add additional context in comments.

12 Comments

Just use str.zfill
@DeepSpace isn't it slower than f-strings? Nonetheless, I prefer f-string because it gives me more flexibility.
I don't know, but even if it is, it is going to be in a very small margin, and f'{str(i).zfill(padding)} My Team {str(i).zfill(padding)}' is much more readable. Of course, if you save str(i).zfill(padding) as a variable the speed difference (if it even exists) will be smaller
@Anna Make sure you are using Python >= 3.6
I just did some timings, and even f'{str(i).zfill(PADDING)} My Team {str(i).zfill(PADDING)}' (without caching str(i).zfill(PADDING)) is faster
|
1

If you're looking to make sure that a number always has six digits, you can do that with something like this

def leadingZeros(num, length):
    out_str = str(num);
    while len(out_str) < length:
        out_str = "0" + out_str;
    return out_str;

1 Comment

Please, drop the semicolons. Also, this is exactly why we have str.zfill
1

You can use string formatting to achieve this

output_string = ''.join([f'{i:06d} My Team {i:06d}\n'for i in range(1, 100001)])

Alternatively using zfill

output_string = ''.join([f'{str(i).zfill(6)} My Team {str(i).zfill(6)}\n'for i in range(1, 100001)])

2 Comments

Just use str.zfill
Why, does zfil has advantages?
0

If you want to print the contents of the file which are already in that format then

with open('file.txt','r') as f:
      file_data=f.read()
      l=file_data.split("\n")
      print(*l)

If you want create a file like that then you can use, place the file the same directory as you code is in

with open('test.txt','w') as myfile:
for i in range(1,100001):
    print("{:0>6d} My team {:0>6d}".format(i,i),file=myfile)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.