Trying to Parse a CSV that has line breaks the way excel does it Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Trying to Parse a CSV that has line breaks the way excel does it

30 Mar 2024, 22:21

Code: Select all

#Requires AutoHotkey v2.0-a
#SingleInstance Force
#Include <JSON>
SetWorkingDir(A_ScriptDir)
csv_text:="
(LTRIM
col1,col2,col3
1,Ben,"my name
is Ben
and here are my details"
2,Jason,My name is jason with 1 line
)"

join(str) {
 out := ""
 Loop Parse, str, "`n", "`r"
  If Ord(A_LoopField) = 32 ; Space
       out .= Trim(A_LoopField)
  Else out .= (out = "" ? "" : "`n") A_LoopField
 Return out
}

msgbox(csv_text)
final_array:=[]
loop parse join(csv_text), "`n"
{
    i:=A_index
    final_array.push([])
    loop parse A_LoopField, "CSV"
    {
        final_array[i].push(A_LoopField)
    }
}
msgbox(JSON.Stringify(final_array))
not working as I expected. Any help will be greatly appreciated
User avatar
mikeyww
Posts: 27009
Joined: 09 Sep 2014, 18:38

Re: Trying to Parse a CSV that has line breaks the way excel does it

31 Mar 2024, 05:30

You have to look at what the code does. Your function looks for a leading space, right? That would need to be changed to accommodate your data's format.

Code: Select all

#Requires AutoHotkey v2.0
csv_text := '
(
col1,col2,col3
1,Ben,"my name
is Ben
and here are my details"
2,Jason,My name is jason with 1 line
)'
MsgBox csv_text '`n`n' join(csv_text), 'Input and output', 'Iconi'

join(str) {
 out := '', more := False
 Loop Parse str, '`n', '`r' {
  StrReplace(A_LoopField, '"',,, &n)  ; n = Number of quotation marks
  out  .= (more ? ' ' : out = '' ? '' : '`n') A_LoopField
  more ^= Mod(n, 2)
 }
 Return out
}
jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Re: Trying to Parse a CSV that has line breaks the way excel does it

31 Mar 2024, 05:39

I'm trying to create 2d array actually. Got the join function from your answer in a ahk 1 thread
User avatar
mikeyww
Posts: 27009
Joined: 09 Sep 2014, 18:38

Re: Trying to Parse a CSV that has line breaks the way excel does it

31 Mar 2024, 07:32

My post updates the "join" function for use with your new data format. You can use it in your script. If you run my demonstration script, it shows you the joined text.
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Trying to Parse a CSV that has line breaks the way excel does it  Topic is solved

31 Mar 2024, 16:30

@jsong55 it is unclear to me what you expect from your script. I also believe the premise might be wrong. When Excel exports to a CSV, cells that include a line break will show a linefeed (`n) while rows are delimited by a carriage return and a linefeed (`r`n). The image clarifies.
Because certain editors (incl. Scite) replace a single `n with `r`n, it is tricky to run your script the way you do. It is unclear whether they are single `n or if they have been replaced with `r`n. It is better to use fileread() as that will preserve the single `n's and not introduce unwanted "`r". That said parsing with the "CSV" option should still be the best way to deal with these files.
So, do you want to eliminate these single linefeeds i.e. "flatten" the cells that have multiple rows (=replace the `n with " ")? or do you want to preserve the linefeeds ?. Do you want to output to a string or to an array?
The code below will do both based on the csv file shown in the picture.

Code: Select all

csv := fileread("testcsv2.csv")

for x,y in strsplit(csv,"`r`n")
	{
	loop parse, y, "csv"
		lst .= (a_index = 1 ? "":",") strreplace(a_loopfield,"`n"," ")
	lst .= "`n" 
	}
msgbox trim(lst,"`n")
	
tbl := []
for x,y in strsplit(csv,"`r`n")
	{
	rw := []	
	loop parse, y, "csv"
		rw.push(a_loopfield)
	(y) && tbl.push(rw)
	}
; show all cells 
for x,y in tbl
	for a,b in y
		msgbox b " (cell " chr(64 + a) x ")"
20240331_132248.jpg
20240331_132248.jpg (19.03 KiB) Viewed 107 times
Attachments
testcsv2.zip
(144 Bytes) Downloaded 6 times
14.3 & 1.3.7
jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Re: Trying to Parse a CSV that has line breaks the way excel does it

01 Apr 2024, 10:07

@flyingDman you hit the nail on the head, just needed that 2d array
jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Re: Trying to Parse a CSV that has line breaks the way excel does it

01 Apr 2024, 10:21

@mikeyww
thanks for your input sorry I wasn't clearer

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: alawsareps, Atom, jaccotjuhhh, ManuelesAdrian and 85 guests