Modify Field In CSV File

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
jeffmazerski
Newbie
Posts: 4
Joined: Wed Apr 07, 2010 2:56 pm

Modify Field In CSV File

Post by jeffmazerski » Fri Apr 10, 2015 7:37 pm

The csv file I am trying to manipulate has a random number of records. The fields I am trying to change are always the 2nd and 4th fields. Basically what I am trying to do is enclose the data in the 2nd and 4th field with a double quote.

So for example, if my record is below
FieldOne,FieldTwo,FieldThree,FieldFour,FieldFive

I would want the output to be
FieldOne,"FieldTwo",FieldThree,"FieldFour",FieldFive

Thanks in advance

hagchr
Automation Wizard
Posts: 330
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Modify Field In CSV File

Post by hagchr » Sat Apr 11, 2015 10:47 am

Hi, this would be a simple way to do it. Hope it helps.

Code: Select all

//For illustration, data is read from label  
LabelToVar>List,StrData,1,0,{"*/"}

//To read data from file use "ReadFile" instead and comment out / delete the "LabelToVar" above  
//ReadFile>C:\...\datafile.csv,strData

Separate>strData,%CRLF%,strRecord

Let>resout=
Let>ct=0
While>ct<StrRecord_count
    Add>ct,1
    Let>tmp0=,
    Separate>strRecord_%ct%,tmp0,strField
    Let>strRecordNew=%strField_1%,"%strField_2%",%strField_3%,"%strField_4%",%strField_5%
    Let>resout=%resout%%strRecordNew%%CRLF%
EndWhile

MDL>resout

//For file output this can be used instead of MDL
/*
IfFileExists>C:\...\ResFile.csv
    DeleteFile>C:\...\ResFile.csv
Endif
WriteLn>C:\...\ResFile.csv,nWLNRes,resout
*/


/*
List:
FieldOne,FieldTwo,FieldThree,FieldFour,FieldFive
FieldOne,,FieldThree,FieldFour,FieldFive
FieldOne,FieldTwo,FieldThree,FieldFour,FieldFive
FieldOne,FieldTwo,FieldThree,FieldFour,FieldFive
FieldOne,FieldTwo,FieldThree,FieldFour,FieldFive
*/

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts