Re: Read a text file
Re: Read a text file
- Subject: Re: Read a text file
- From: Stan Cleveland <email@hidden>
- Date: Tue, 29 Jul 2014 12:42:02 -0700
On Jul 29, 2014, at 12:25 PM, Gil Dawson wrote: I have a sort of generic way of processing a plain text file line-by-line...
However, I'm running into problems:
1. Sometimes, the EOL delimiter is not return, but LF or CR/LF. 2. Sometimes, the encoding can be UTF-8, sometimes UTF-8 with BOM, sometimes UTF-16.
Is there a way for me to tell a priori what are the EOL delimiter and encoding for the file?
I can open the file in Text Wrangler and then test its text document's line breaks and encoding properties. But I thought perhaps there's something in the native Mac/AppleScript world that could do this.
Any suggestions?
Hi Gil,
Your code is doing a lot of unneeded work. Here's a simplified approach that should do everything you need:
set srtFile to (choose file with prompt "Select a file:") set docLines to paragraphs of (read srtFile) repeat with i from 1 to (count docLines) set srtLine to item i of docLines -- do something with this line end repeat
This should work as long as you're not trying to process files with tens of thousands of lines. For files that large, I'd be using 'do shell script' to make calls to grep, sed or awk, depending on what processing was needed.
Stan C.
|
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden