Created
May 22, 2020 06:50
-
-
Save EvanMu96/bca62a8bd8f6063e75c22c76fa39bd7f to your computer and use it in GitHub Desktop.
recovery the csv file destroy by microsoft excel
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
# scrip to recovery a csv file ruined by Excel | |
# usage python recovery.py [filename.csv] | |
import re | |
import sys | |
def recovery(line, n_f_lines): | |
n_line = re.sub(r"\n", "ENTER", line) | |
n_line = re.sub(r"\s+", ",", n_line) | |
n_line = re.sub("ENTER", "\n", n_line) | |
n_f_lines.append(n_line) | |
def main(): | |
file_name = sys.argv[1] | |
csv_suff = ".csv" | |
if(not file_name.endswith(csv_suff)): | |
print("This file is not a valid csv file") | |
exit() | |
f = open(file_name, "r") | |
n_f = open(file_name + ".output", "w+") | |
n_f_lines = [] | |
for line in f: | |
recovery(line, n_f_lines) | |
n_f.writelines(n_f_lines) | |
f.close() | |
n_f.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment