Skip to content

Instantly share code, notes, and snippets.

@salman2learn
Last active April 19, 2021 13:53
Show Gist options
  • Save salman2learn/4d41bee151cec0602361fb577bc0d0ec to your computer and use it in GitHub Desktop.
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.
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