탭을 열 구분 기호로 사용하여 Excel에서 데이터를 가져옵니다 (데이터> 텍스트 파일에서로드). 그런 다음 파일을 csv로 저장하십시오.
호환성 문제가있을 수 없으며, 기본 작업이며 과거에 자주 수행했습니다.
import csv
# read tab-delimited file
with open('yourfile.tsv','rb') as fin:
cr = csv.reader(fin, delimiter='\t')
filecontents = [line for line in cr]
# write comma-delimited file (comma is the default delimiter)
with open('yourfile.csv','wb') as fou:
cw = csv.writer(fou, quotechar='', quoting=csv.QUOTE_NONE)
cw.writerows(filecontents)
예 인터프리터 세션 :
>>> import csv
>>> with open('yourfile.tsv','rb') as fin:
... cr = csv.reader(fin, delimiter='\t')
... filecontents = [line for line in cr]
...
>>> with open('yourfile.csv','wb') as fou:
... cw = csv.writer(fou, quotechar='', quoting=csv.QUOTE_NONE)
... cw.writerows(filecontents)
...
>>> with open('yourfile.csv','rb') as see_how_it_turned_out:
... for line in see_how_it_turned_out:
... line
...
'attribute1,attribute2,attribute3,attributeN\r\n'
'value"A",value"B",value"C",value"Z"\r\n'
주 : 당신이 scripting language를 사용할 수있는 경우
CSV는 매우 정의 된 형식이 아닙니다. 어떤 것은 ";" 구분자로 ","를 사용하십시오. 날짜 서식도 매우 다양하며 문자열을 구분하는 옵션도 있습니다. 클라이언트 (사람 또는 프로세스)와 함께 이러한 요구 사항을 명확히해야합니다. –