Page 1 of 1 1
Topic Options
#177252 - 2007-06-26 07:30 PM find/replace
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
What is the best way to do a find/replace from a csv file? I'm assuming that I need to Open the file, do a SPLIT on the fields and then somehow do a find/replace on the array and then Join the array back to a csv format. Am I on the right track or is there a better way?

Thanks.

Top
#177253 - 2007-06-26 07:52 PM Re: find/replace [Re: booey]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
If your csv file for example looks like this.

 Quote:

11;22;33;AA;44;55
22;XX;33;BB;ZZ;00


You could do something like this.
The AA in the first line will be replaced by BB. The other lines stays as it is.
There also I a replace UDF (two in fact) one of them does exactly the same as the join split line below.

 Code:
Break on

$rc = Open (1, "c:\test.csv", 2)
$rc = Open (2, "c:\test2.csv", 5)

$line = ReadLine (1)
While @ERROR = 0
	If InStr($line, "AA")
		$line = Join(Split($line,"AA"),"BB")
		$rc = WriteLine (2, $line + @CRLF)
	Else
		$rc = WriteLine (2, $line + @CRLF)
	EndIf
	$line = ReadLine (1)
Loop

$rc = Close (1)
$rc = Close (2)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#177288 - 2007-06-27 01:55 PM Re: find/replace [Re: Mart]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Mart,
What if the file looks like
 Code:
11;AA22;833;AA;44;5CF5
22;XX58;233;BB;ZZ;0AA0

I think you better split the lines on the ";", AScan the array for an exact "AA" match and join the array using ";" as delimiter.

Top
#177289 - 2007-06-27 02:18 PM Re: find/replace [Re: Witto]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Yep. You got me on that one
But like you said "What if..." I guess we will know when the original poster shows some examples of the actual csv file.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#177298 - 2007-06-27 05:56 PM Re: find/replace [Re: booey]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
You're on the right track, but CSV files are potentially more complex than data separated by commas. You should use the CSV() udf to convert the CSV string to an array, examine and possibly change the values in the array, and pass the array back through CSV again to return it to a proper CSV string. Here's some conceptual code that checks the third field for the update:
 Code:
; rename the original file
Ren 'mydata.csv' 'temp.csv'
; open the work file
Open(2,'temp.csv')
; send output to the original file
$ = RedirectOutput('mydata.csv')
; read a line, convert CSV data to an array
$CSV = CSV(ReadLine(2))
While Not @ERROR
  ; if the array value matches, change it
  ; I'm assuming the third field in this example
  ; look for the value
  If Not InStr($CSV[3], 'change_me')
    $CSV[3] = 'Change_me'        ; change it
  EndIf

  ; Add the CSV string to the output file - pass the array back to CSV()
  ; the returned string will be redirected to the file
  CSV($CSV) ?

  ; get the next line into an array
  $CSV = CSV(ReadLine(2))

Loop

$ = Close(2)
$ = RedirectOutput('')

; delete the work file, unless you want to keep a backup
Del 'temp.csv'


You'll need to make the appropriate changes to the part of the code that checks foe the "bad" value and changes it to the "good" value, if it's any more complex that replace "X" with "Y", but the base logic here should work just fine.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 857 anonymous users online.
Newest Members
JimDunn, fgarcia, Viginette, ManuvdWielNL, Sir_Barrington
17890 Registered Users

Generated in 0.028 seconds in which 0.011 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org