Last active
November 20, 2018 13:40
-
-
Save greentec/146e0a76bf9604af33741ea87823b682 to your computer and use it in GitHub Desktop.
save csv with row value
This file contains 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
with open('sample.csv') as f: | |
prev = [] | |
for line in f.readlines(): | |
print(prev, line) | |
if len(prev) == 0: | |
prev.append(line) | |
elif prev[-1] != ','.join(line.split(',')): | |
file_name = ''.join(prev[-1].strip().split(',')) + '.csv' | |
with open(file_name, 'w') as new_file: | |
for prev_line in prev: | |
new_file.write(prev_line) | |
prev = [line] | |
else: | |
prev.append(line) | |
if len(prev) > 0: | |
file_name = ''.join(prev[-1].strip().split(',')) + '.csv' | |
with open(file_name, 'w') as new_file: | |
for prev_line in prev: | |
new_file.write(prev_line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment