Skip to content

Instantly share code, notes, and snippets.

@EvanMu96
Created May 22, 2020 06:50
Show Gist options
  • Save EvanMu96/bca62a8bd8f6063e75c22c76fa39bd7f to your computer and use it in GitHub Desktop.
Save EvanMu96/bca62a8bd8f6063e75c22c76fa39bd7f to your computer and use it in GitHub Desktop.
recovery the csv file destroy by microsoft excel
# 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