split string with csv file golang

huangapple go评论79阅读模式
英文:

split string with csv file golang

问题

我正在尝试从Excel电子表格中提取日期,我已经成功获取到一个包含完整日期的日志,我想知道如何只提取月份。例如,在这种情况下,日志中是这样写的:"log.Println("DT REFER", emp.DT_REFER)",其中DT_REFER是电子表格中的日期,我该如何只提取月份?日期的格式是这样的:DT REFER 2011-03-31。

英文:

I'm trying to extract a date from an excel spreadsheet, I managed to give a log where it shows the entire date, I wanted to know how to extract only the month, for example, in this case it's "log.Println("DT REFER", emp. DT_REFER" the DT_REFER is where the date is in the spreadsheet, how can I get only the month out of it? the format that came was this.
DT REFER 2011-03-31

答案1

得分: 1

你可以使用fmt.Sscanf来从你的日志字符串中提取月份。假设你有一个固定的字符串结构可以进行匹配,代码如下:

// 如果emp.DT_REFER不是字符串,请使用Sprint将其转换为字符串
s := fmt.Sprint(emp.DT_REFER)
y, m, d := 0, 0, 0
fmt.Sscanf(s, "%d-%d-%d", &y, &m, &d)
fmt.Println(y, m, d)
英文:

You can use fmt.Sscanf to extract the month out of the log string that you have. Given you have a fixed string structure that you can match against as follows:

// if emp.DT_REFER isn't string use Sprint to make it string
s := fmt.Sprint(emp.DT_REFER)
y, m, d := 0, 0, 0
fmt.Sscanf(s, "%d-%d-%d", &y, &m, &d)
fmt.Println(y, m, d)

huangapple
  • 本文由 发表于 2022年2月25日 03:59:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/71257582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定