Python Forum
Adding string after every 3rd charater [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding string after every 3rd charater [SOLVED]
#1
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?
Reply
#2
(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 :)
BashBedlam likes this post
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into </>? Winfried 0 297 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 308 Aug-19-2022, 08:07 PM
Last Post: Winfried
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 1,257 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Replace String in multiple text-files [SOLVED] AlphaInc 5 4,024 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  Replace String with increasing numer [SOLVED] AlphaInc 13 2,883 Aug-07-2021, 08:16 AM
Last Post: perfringo
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 1,418 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  Adding an ascending number [SOLVED] AlphaInc 3 1,382 Jul-11-2021, 10:13 AM
Last Post: perfringo
  reading lines from a string [Solved] ebolisa 14 4,016 Mar-28-2021, 08:16 PM
Last Post: perfringo
  Adding markers to Folium map only adding last element. tantony 0 1,577 Oct-16-2019, 03:28 PM
Last Post: tantony
  adding a string and an int ninjarunner2005 3 1,739 Dec-10-2018, 04:54 PM
Last Post: buran

Forum Jump:

User Panel Messages

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