Last active
April 19, 2021 13:53
-
-
Save salman2learn/4d41bee151cec0602361fb577bc0d0ec to your computer and use it in GitHub Desktop.
Reshape data to make consistent column count. Line by line operation without high memory usage.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reshape(srcFilePath, destFilePath, delim, colCount, filler=''): | |
with open(srcFilePath, 'r') as src, open(destFilePath, 'w') as dest: | |
for line in src: | |
arr = line.split(delim) | |
if len(arr) < colCount: | |
line = line.strip() + delim*(colCount-len(arr)) | |
if len(arr) > colCount: | |
line = delim.join(arr[:colCount]) | |
dest.write(f'{line}\n') | |
reshape('src.txt', 'dest.txt', '||', 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment