2012-04-10 4 views
2

CSV에서 데이터를 읽는 데 사용하는 applescript가 있습니다 (성공적으로!). 데이터 편집을 마친 스크립트가 끝나면 수정 된 텍스트를 해당 파일에 다시 쓰려고합니다. 내 텍스트가 CSV 파일에 다시 쓰여지면 모든 캐리지 리턴이 손실되어 한 줄로 읽습니다. 내가 뭘 잘못 했니?Applescript를 사용하여 CSV에 목록 작성하기

set theFile to choose file with prompt "Select a text file:" 
set theFileContents to read theFile 
... 


set theList to paragraphs of theFileContents 



repeat with i from 2 to count theList 

set AppleScript's text item delimiters to "," 
set theLine to theList's item i 
theLine 
set clinic_id to first text item of theLine 
.... 
set AppleScript's text item delimiters to "" 

... 

    open for access theFile with write permission 

    set theData to theList as text 
    write the [theData] to theFile as text 
    close access theFile 


    display dialog the [theLine] 
end if 
end repeat 

의견이 있으십니까? 이 코드의 출력은 다음과 같습니다 내가 원하는

1,2,3 
A,B,C 
X,Y,Z 
+0

내가 잘못하지 않았다면,'theList' 변수는 원래 파일의 모든 단락 (줄)을 목록으로 가지고 있으므로 루프 * 안에 파일 *에 쓰면 파일의 전체 내용 *을 쓸 것입니다 줄을 여러 번 가져라. 아마도'end repeat' * 후에 파일 *에 쓰는 것이 더 합리적일까요? – fanaugen

답변

3

1,2,3,A,B,C,X,Y,Z 

경우가 {"1,2,3", "A,B,C", "X,Y,Z"}처럼 보이는, 그래서 내가 당신의 theList이 항목으로 (각 라인 쉼표로 문자열을) 선이 가정, 권리?

set AppleScript's text item delimiters to "\n" 
set theData to {"1,2,3", "A,B,C", "X,Y,Z"} as text 

theData 지금 정확하게 값

"1,2,3 
A,B,C 
X,Y,Z" 

있다 : 그래서, 파일에 기록하기 위해 text에 목록을 변환 전에 개행 문자로 text item delimiters 설정 출력 파일에서 무엇을 발견 할 것인가.