[Newbies] Splitting Excel csv into lines

stephan at stack.nl stephan at stack.nl
Wed Dec 24 13:32:34 UTC 2008


String lines doesn't handle separating Excel copy-and-paste very well,
because soft enters in a cell get splitted into separate lines.

Ive done now:

splitIntoLines: aString
	"Return a collection with the string-lines of the receiver."

	| input char temp inQuote|
	input := aString readStream.
	^ Array streamContents: [ :output |
		temp := ''.
		inQuote := false.
		[ input atEnd ] whileFalse: [
			char := input next.
			char = $" ifTrue: [
				inQuote ifTrue:[
					input peek = (Character tab) ifTrue: [
						char := input next.].
					input peek = (Character cr) ifTrue: [
						char := input next.
						inQuote := false]]].
			char = (Character tab) ifTrue:[
				inQuote ifTrue: [ inQuote := false]
				ifFalse: [
					input peek= $" ifTrue: [
						input next.
						inQuote:=true]]].
			char = (Character cr)
				ifFalse: [temp := temp, char asString]
				ifTrue: [
					inQuote ifFalse: [
						output nextPut: temp.
						temp:=''.
						input peek = Character lf ifTrue: [input next]]]]]

I would be interested in (speed & elegance & mistakes) improvements to it.

Stephan


More information about the Beginners mailing list