• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Importing/parsing CSV files
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Importing/parsing CSV files


  • Subject: Re: Importing/parsing CSV files
  • From: kai <email@hidden>
  • Date: Wed, 25 Oct 2006 01:09:39 +0100


On 24 Oct 2006, at 03:57, T&B wrote:

Last month, you posted an excellent, fast CSVToList solution, using AppleScript Text item delimiters, temporarily ASCII character substitutions, and a list within a script object property. The original post is appended below.

Unfortunately, it has a bug. When it replaces "" with the temporary quoteSubstitute (q), which are later replaced with " (one quote), it also distorts empty items. For instance the third item in this example:

item1,"quoted item","","that was an empty item","but processes ""this"" correctly"

which becomes a list:

{"item1","quoted item","\"","that was an empty item","but processes \"this\" correctly"}

but should be this (third item changed):

{"item1","quoted item","","that was an empty item","but processes \"this\" correctly"}

I see what you mean, Tom.

IIRC, the original suggestion assumed that empty fields would be represented by multiple commas, à la Excel - so the question of empty quotes wasn't really considered. Well spotted.

I fixed it by inserting a repeat loop, as per:

I was wondering if we could perhaps come up with a sans-repeat fix for this, so I've inserted a suggested mod below that uses text item delimiters instead. Brief testing suggests that it works - and it should prove a tad faster than a loop. However, I'll leave you to test it in a real world situation... :)


on CsvToList(t)
   set quot to "\""
   set comma to ","
   set oldDelimiters to text item delimiters
   set quoteSubstitute to ASCII character 0
   set newLineSubstitute to ASCII character 1
   set commaSubstitute to ASCII character 2
   set text item delimiters to quot & quot
   set t to t's text items
   set text item delimiters to quoteSubstitute
   set t to t as string

(* suggested alternative fix *)

    set text item delimiters to comma & quoteSubstitute & comma
    set t to t's text items
    set text item delimiters to comma & comma
    set t to t as string	

   set text item delimiters to quot
   script o
      property l : t's text items
   end script
   repeat with i from 1 to count o's l by 2
      set text item delimiters to comma
      set t to text items of o's l's item i

(* repeat loop removed from here *)

      set text item delimiters to commaSubstitute
      set t to t as string
      set text item delimiters to newLineSubstitute
      set o's l's item i to t's paragraphs as string
   end repeat
   set text item delimiters to ""
   set t to o's l as string
   set text item delimiters to quoteSubstitute
   set o's l to t's text items
   set text item delimiters to quot
   set t to o's l as string
   set text item delimiters to newLineSubstitute
   set o's l to t's text items
   set text item delimiters to commaSubstitute
   repeat with i from 1 to count o's l
      set o's l's item i to text items of o's l's item i
   end repeat
   set text item delimiters to oldDelimiters
   return o's l
end CsvToList

--- kai


_______________________________________________ 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/mailman//archives/applescript-users

This email sent to email@hidden
References: 
 >Re: Importing/parsing CSV files (From: T&B <email@hidden>)

  • Prev by Date: Re: Get filestructure in a variable
  • Next by Date: Re: How can I open attachment in Mail ?
  • Previous by thread: Re: Importing/parsing CSV files
  • Next by thread: Scripting Capture of an Internet Connect Window?
  • Index(es):
    • Date
    • Thread