Fileappend in CSV but getting Issue with Comma

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Pardeep Tariyal
Posts: 61
Joined: 30 Aug 2017, 08:43

Fileappend in CSV but getting Issue with Comma

22 Jun 2018, 07:25

I am getting comma issue while appending file to csv. it is working fine until or unless any value has comma itself. The data which I want to read have some values with comma like Name. When i am reading the data and appending into csv, the values (Name) being separated.

Code: Select all

G_List =
(
6/22/2018	WH10301963618201892082	22764	PERROTTA, WHITNEY	5903	PERROTTA, PETER
6/22/2018	EL4151986617201892082	22763	VANDERVALK, ELIZABETH	13003	-
6/22/2018	AD9211997618201892082	22762	VU, ADAM	13002	VU, HUNG
)

Loop, parse, G_List, `n, `r
{
Stringsplit, New_List, A_LoopField, %A_Tab%
fileappend, %New_List1%`,%New_List2%`,%New_List3%`,%New_List4%`,%New_List5%`,%New_List6%`,%New_List7%`n, %A_ScriptDir%\new.csv
}
msgbox done
return
Guest

Re: Fileappend in CSV but getting Issue with Comma

22 Jun 2018, 08:32

I recommend using Format4CSV() by Rhys which takes cares of commas and quotes:

Code: Select all

; Format4CSV by Rhys
; http://www.autohotkey.com/forum/topic27233.html
Format4CSV(F4C_String)
{
   Reformat:=False ;Assume String is OK
   IfInString, F4C_String,`n ;Check for linefeeds
      Reformat:=True ;String must be bracketed by double quotes
   IfInString, F4C_String,`r ;Check for linefeeds
      Reformat:=True
   IfInString, F4C_String,`, ;Check for commas
      Reformat:=True
   IfInString, F4C_String, `" ;Check for double quotes
   {   Reformat:=True
      StringReplace, F4C_String, F4C_String, `",`"`", All ;The original double quotes need to be double double quotes
   }
   If (Reformat)
      F4C_String=`"%F4C_String%`" ;If needed, bracket the string in double quotes
   Return, F4C_String
}
which also included in this CSV library here https://github.com/hi5/CSV
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Fileappend in CSV but getting Issue with Comma

22 Jun 2018, 08:57

Hello.

I have added a "masking"-function, which I have found inside csv-library.

I recommend using whole library.

Code: Select all

G_List =
(
6/22/2018	WH10301963618201892082	22764	PERROTTA, WHITNEY	5903	PERROTTA, PETER
6/22/2018	EL4151986617201892082	22763	VANDERVALK, ELIZABETH	13003	-
6/22/2018	AD9211997618201892082	22762	VU, ADAM	13002	VU, HUNG
)

Loop, parse, G_List, `n, `r
{
Stringsplit, New_List, A_LoopField, %A_Tab%
fileappend, % Format4CSV(New_List1) "," Format4CSV(New_List2) "," Format4CSV(New_List3) "," Format4CSV(New_List4) "," Format4CSV(New_List5) "," Format4CSV(New_List6) "," Format4CSV(New_List7) "`n", %A_ScriptDir%\new.csv
}
msgbox done
return


; Format4CSV by Rhys
; HTTP://WWW.AUTOHOTKEY.COM/FORUM/TOPIC27233.HTML
Format4CSV(F4C_String)
{
   Reformat:=False ;Assume String is OK
   IfInString, F4C_String,`n ;Check for linefeeds
      Reformat:=True ;String must be bracketed by double quotes
   IfInString, F4C_String,`r ;Check for linefeeds
      Reformat:=True
   IfInString, F4C_String,`, ;Check for commas
      Reformat:=True
   IfInString, F4C_String, `" ;Check for double quotes
   {   Reformat:=True
      StringReplace, F4C_String, F4C_String, `",`"`", All ;The original double quotes need to be double double quotes
   }
   If (Reformat)
      F4C_String=`"%F4C_String%`" ;If needed, bracket the string in double quotes
   Return, F4C_String
}
Einfach nur ein toller Typ. :mrgreen:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 215 guests