Picking elements out of CSV file Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Picking elements out of CSV file

Post by roysubs » 16 May 2022, 10:56

I worked out how to capture a CSV from a Google Sheets page, and I want to perform some manipulations on it
Here is a snippet of the output:

Code: Select all

1,01/01/2022,GamesJart,GalahadB,Base,ladder,,
2,01/01/2022,branstonpickle,YanBee,Base,ladder,https://drive.google.com/open?id=1x_SVKxLwdhHWm0kpoB4FJgvAOXfHADzhR,
3,01/01/2022,Terminatorhenk,Bomber,Base,ladder,,
4,01/01/2022,Barbarisco,RoySubs,Base,ladder,,
5,,,,,,,
6,,,,,,,
7,01/01/2022,branstonpickle,YanBee,Base,ladder,https://drive.google.com/open?id=1x_SVKxLwdhHWm0kpoB4FJgvAOXfHADzhR,
8,,,,,,,
9,,,,,,,
10,,,,,,,
11,,,,,,,
I want to strip / delete all of the lines with empty url field (i.e. the last field, so anything like these)

Code: Select all

4,01/01/2022,Barbarisco,RoySubs,Base,ladder,,
5,,,,,,,
Then, for each line, how do I capture date (2nd field), then name1 (3rd), then name2 (4th), then mode (5th), then type (6th), then url (7th) ?

I want to put everything into a GUI, showing just fields 2,3,4,5,6 per line, so that someone can pick a line and then, I will get that URL, then download the item at the URL and open that file (which should be hopefully be easy once I can pick out the elements)

Any help with slicing and dicing CSV's like this would be appreciated.

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Picking elements out of CSV file

Post by mikeyww » 16 May 2022, 11:15

Loop, Parse has a handy CSV option. You can use it to read your fields, and then skip your special action when the last field is null. Example

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Picking elements out of CSV file

Post by BoBo » 16 May 2022, 11:21

Code: Select all

#SingleInstance, Force
FileRead, content, my.csv

Loop, parse, content, `r`n
   {	line := StrSplit(A_LoopField,",")
   		lines .= if (line.7 = "") ? "" : line.2 "|" line.3 "|" line.4 "|" line.5 "|" line.6 "|" line.7 "`n"
   }
MsgBox % RTrim(lines,"`n")
Not tested.

User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Picking elements out of CSV file  Topic is solved

Post by Xtra » 16 May 2022, 12:39

BoBo wrote:
16 May 2022, 11:21
Not tested.
Using line := StrSplit(A_LoopField,",") on a CSV file is bad practice.
You are assuming all cells will never have a comma.

Basic example to build on. Parse row/line and then each column/cell:

Code: Select all

Loop, Parse, myCSV, `n, `r
{
    row := A_LoopField
    Loop, Parse, row, CSV
    {
        col := A_LoopField
        ; Use switch or if else here and check for condition on col
    }
}

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Picking elements out of CSV file

Post by BoBo » 16 May 2022, 14:48

@Xtra
You are assuming all cells will never have a comma.
Nope. I'm assuming that it's bad practice to use commas as separators if there's any intention to use commas as its content. Maybe that's the reason why people are using all sorts of delimiters...
"Semicolons are often used instead of commas in many European locales in order to use the comma as the decimal separator and, possibly, the period as a decimal grouping character."
Source: https://en.wikipedia.org/wiki/Comma-separated_values
:)

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Picking elements out of CSV file

Post by mikeyww » 16 May 2022, 15:00

OK about the diversity of possible delimiters.

We are handed a CSV file. In such a file, cells containing the delimiter itself would typically be quoted, such as in the following four-cell file.

Code: Select all

a,b,"c,d,e",f
(Or all cells might be quoted.)

Various scripts could parse this, but AHK's CSV option would handle such a file "natively", as this option is designed for this type of file. Using the delimiter alone to parse the file would lead to unexpected results.

roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Re: Picking elements out of CSV file

Post by roysubs » 19 May 2022, 14:01

Thanks all! :salute:
All really useful, and I'm getting the elements all extracted nicely now and able to work with them...
Cheers,
Roy.

Post Reply

Return to “Ask for Help (v1)”