英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论