Created
July 17, 2017 22:06
-
-
Save pedrotnascimento/4eae28915605ab5b43e3801c65380e58 to your computer and use it in GitHub Desktop.
algoritmo para tratar \r\n de textos livres em tabelas csv.
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
# -*- coding: utf-8 -*- | |
# algoritmo para tratar \r\n de textos livres em tabelas csv. | |
# requirimentos | |
# delimitador = ";" | |
# newline =\r\n | |
# a ultima coluna(antes do new line) precisa estar garantido não ter \r\n, para garantir a saida do loop | |
with open("abc.csv", "rb") as fr: | |
h = fr.read().strip() | |
lines = h.split("\r\n") | |
# lines = [lines[0]] + lines[2:len(lines)-2] | |
col_len = len(lines[0].split(";")) | |
i =0 | |
try: | |
print len(lines), col_len | |
while i<len(lines): | |
while len(lines[i].split(";")) != col_len: | |
print lines[i] | |
print len(lines[i].split(";")) | |
lines[i] = lines[i].replace("\r\n", "") | |
lines[i] += lines[i+1] | |
del lines[i+1] | |
print lines[i] | |
input() | |
i+=1 | |
except: | |
print "total de linhas", len(lines), "\nerro na linha", i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment