0

I'm working on a program that is supposed to read the contents of a file and extract the data from it. I've tried to use fmt.Fscanf() to scan the contents line by line, but for some reason, I can't get it to work correctly. The entire line gets scanned into the first string argument only. What am I doing wrong here?

I'm assuming it's not working because the values aren't separated by whitespaces but I couldn't find a solution. Thank you for your help and expertise!

20220105|AA|1051302|4323|3132468|B,Q,N
20220105|AAA|61|0|62|Q
20220105|AAAU|19404|0|57137|Q,N
20220105|AAC|35524|0|39861|Q,N
20220105|AAC/WS|1180|0|2000|N
20220105|AACG|1805439|32577|3484265|B,Q,N
func main() {
  file, err := os.Open("./CNMSshvol20220105.txt")
  if err != nil{
    log.Fatal(err)
  }
  defer file.Close()

  var m map[string][]int
  m = make(map[string][]int)

  for{
    row := ""
    date := ""
    symbol := ""
    shortVolume := 0
    shortExemptVolume := 0
    totalVolume := 0
    markets := ""

    //Date|Symbol|ShortVolume|ShortExemptVolume|TotalVolume|Market
    var n int
    n, err := fmt.Fscanf(file, "%s\n", &row)
    fmt.Sscanf(row, "%s|%s|%d|%d|%d|%s", &date, &symbol, &shortVolume, &shortExemptVolume, &totalVolume,&markets)
    fmt.Println(date)
    if n == 0 || err != nil{
      log.Fatalf("Fscanf: %v\n", err)
      break
    }
    fmt.Println(symbol, shortVolume, shortExemptVolume, totalVolume)
    m[strings.ToLower(symbol)] = append(m[strings.ToLower(symbol)],shortVolume)
    m[strings.ToLower(symbol)] = append(m[strings.ToLower(symbol)],shortExemptVolume)
    m[strings.ToLower(symbol)] = append(m[strings.ToLower(symbol)],totalVolume)

  }

  //Retrieve values if the key exists
  for{
    fmt.Println("\n\n\n\nEnter ticker to retrieve short volume information.")
    var s string
    fmt.Scanf("%s",&s)
    data, ok := m[strings.ToLower(s)]
    if !ok{
      fmt.Println("Not Found.")
      continue
    }
    for _, value := range data{
      fmt.Println(value)
    }
  }


}```

1 Answers1

3

fmt.Sscanf documentation says: Sscanf scans the argument string, storing successive space-separated values into successive arguments as determined by the format

| character is not space and each format placeholder is independent of any other, so %s| does not mean match any string up to a first | but instead match any string not including space, and then a |.

You can process file line by line, split each line e.g using strings.Split() by | and then parse values into variables as you need:

row := "20220105|AACG|1805439|32577|3484265|B,Q,N"
tokens := strings.Split(row, "|")
if len(tokens) != 6 {
    log.Fatal("Bad line in file...")
}

// Do appropriate conversions...
date, err := strconv.Atoi(tokens[0])
if err != nil {
    log.Fatal("first token is not an integer")
}
symbol := tokens[1]
// ...

fmt.Println(date, symbol)

or as Volker pointed out in his comment you can use package encoding/csv and set its Reader Comma (separator) option to |:

f := `20220105|AA|1051302|4323|3132468|B,Q,N
20220105|AAA|61|0|62|Q`

r := csv.NewReader(strings.NewReader(f))
r.Comma = '|'
data, err := r.ReadAll()
if err != nil {
    log.Fatal(err)
}

// Process data, do appropriate conversions

fmt.Print(data)
blami
  • 6,588
  • 2
  • 23
  • 31