Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def corrector (string, sep=","):
- '''
- Функция разбиения на строки файлов csv.
- string -- строка файла csv.
- sep -- разделитель.
- '''
- escaped = False # Метка экранирования
- out = []
- buff = [] # Буфер в который будут записываться строки
- for i in string.split(","):
- if '"' in i : # От этого условия можно в принципе и отказаться
- i_index = 0
- while i_index != -1:
- i_index = i.find('"', i_index)
- # Наверное правильнее было бы использовать index,
- # но мне лень.
- if i_index != -1:
- if i_index == len(i)-1:
- # Очевидно, что если кавычки последние в строке,
- # то они единичные
- escaped = not (escaped)
- i_index = -2 # Да, знаю, грязный хак. Но лень...
- else:
- if i[i_index+1] == '"':
- i = i[:i_index] + i[i_index+1:]
- else:
- escaped = not (escaped)
- i_index = i_index+1
- else:
- buff.append(i)
- else:
- buff.append(i)
- if not(escaped):
- # Сброс буфера
- out.append(",".join(buff))
- buff = []
- return out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement