Python Forum
Adding string after every 3rd charater [SOLVED] - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Adding string after every 3rd charater [SOLVED] (/thread-37696.html)



Adding string after every 3rd charater [SOLVED] - AlphaInc - Jul-10-2022

Hello everybody,

I want to get the filesize of a folder and store it as a variable. So far I've manged to do so:

import os

def get_size(start_path = '.'):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            if not os.path.islink(fp):
                total_size += os.path.getsize(fp)
    return total_size

line = get_size()
print(line)
Now I want to expand on that so that, going from the end to beginning, after every 3 digits it adds a point.
For example, instead of 1515124421 I want it to look like 1.515.124.421

How can I do so?


RE: Adding string after every 3rd charater. - AlphaInc - Jul-10-2022

(Jul-10-2022, 01:34 PM)AlphaInc Wrote: Hello everybody,

I want to get the filesize of a folder and store it as a variable. So far I've manged to do so:

import os

def get_size(start_path = '.'):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            if not os.path.islink(fp):
                total_size += os.path.getsize(fp)
    return total_size

line = get_size()
print(line)
Now I want to expand on that so that, going from the end to beginning, after every 3 digits it adds a point.
For example, instead of 1515124421 I want it to look like 1.515.124.421

How can I do so?

Alright, I got it myself. If anyone is interested in it:

#!/usr/bin/env python3

#Imports
import sys
import os

#Get filesize
def get_size(start_path ='.'):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            if not os.path.islink(fp):
                total_size += os.path.getsize(fp)
    return total_size

#Convert filesize
tmp_size = str(int(get_size()))[::-1]

#Reverse filesize
def convert_size(string, n):
    with_asterisks = []
    characters_scanned = 0
    for character in string:
        if characters_scanned == n:
            with_asterisks.append('.')
            characters_scanned = 0
        characters_scanned += 1
        with_asterisks.append(character)
    return ''.join(with_asterisks)

#get final filesize
fnl_size = convert_size(tmp_size, 3)[::-1]

print(fnl_size)

#End
sys.exit()
If (which I think will be the case) this is too complex and there is a far easier method, please let me know :)


RE: Adding string after every 3rd charater [SOLVED] - ibreeden - Jul-11-2022

(Jul-10-2022, 03:15 PM)AlphaInc Wrote: If (which I think will be the case) this is too complex and there is a far easier method, please let me know :)
Congratulations, well done. But indeed there is an easier method: use an f-string and mark the type as "n" (number). But then you need to set the locale of your environment. (Not sure if this works also with Windows.)
PyDev console: starting.
Python 3.9.5 (default, Nov 23 2021, 15:27:38) 
[GCC 9.3.0] on linux

import locale
locale.setlocale(locale.LC_ALL, '')  # Get the locale from your OS
'nl_NL.UTF-8'
aa = 1234567890
print(f"{aa:n}")
1.234.567.890