0
다음 함수에서 템플릿에서 파일을 업로드하고 다음 함수에 전달합니다. \ n 또는 \ t (This is a tab separated file
)가 있으면 데이터가 손상됩니다.Django python 이스케이프 n 문자
1. \ n 또는 일부 특수 문자가있는 경우 다음 행에 데이터를 저장합니다.이를 방지하려면 어떻게해야합니까?
2.data가 없음 또는 데이터하지 않습니다! = ""여전히 널 값
def save_csv(csv_file,cnt):
ret = 1
arr = []
try:
flag = 0
f = open(csv_file)
for l in f:
if flag == 0:
flag += 1
continue
parts = l.split("\t")
counter = 1
if(len(parts) > 6):
ret = 2
else:
taggeddata = Taggeddata()
for data in parts:
data = str(data.strip())
if counter == 1 and (data is not None or data != ""):
taggeddata.field1 = data
elif counter == 2 and (data is not None or data != ""):
taggeddata.field2 = data
elif counter == 3 and (data is not None or data != ""):
taggeddata.field3 = data
elif counter == 4 and (data is not None or data != ""):
taggeddata.field4 = data
elif counter == 5 and (data is not None or data != ""):
taggeddata.field5 = data
elif counter == 6 and (data is not None or data != ""):
taggeddata.field6 = data
counter += 1
taggeddata.content_id = cnt.id
taggeddata.save()
arr.append(taggeddata)
return ret
except:
write_exception("Error while processing data and storing")
바퀴를 재발견하지 마십시오 [CSV 모듈]를 사용 (http://docs.python.org/library/csv.html) CSV 파일을 다루는 , 모듈은 적절한 이스케이프 및 기타 문제를 처리 할 수 있습니다. –