Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Sunday, September 14, 2025

Split large file into mutiple files via Python

Below python code shows how to split large file into multiple small files. Here reads ach 1000 lines from files and write into small file. 


from unidecode import unidecode
lines_per_file = 1000
smallfile = None
with open('large-file.txt',encoding="utf8") as bigfile:
    for lineno, line in enumerate(bigfile):
        if lineno % lines_per_file == 0:
            if smallfile:
                smallfile.close()
            small_filename = 'small_file_{}.txt'.format(lineno + lines_per_file)
            smallfile = open(small_filename, "w", encoding="utf-8")
        smallfile.write(line)
    if smallfile:
        smallfile.close()

Wednesday, June 16, 2021

Currency Format of Numeric Values in Pandas

Firstly, Convert column into float type 

data['Amount'] = data['Amount'].fillna(0).astype('float64') 

Format column as Currency format. eg : 12,345.00

data['Amount'] = data['Amount'].map('{:,.2f}'.format)