我正在尝试从特定格式的文件中读取数据。
文件如下所示
title:stack|content:overflow|metadata:53|comments:none
title:google|content:website|metadata:213|comments:Demos
我需要逐行阅读这一行,并为每一行分配title值为 ( "stack") 的标题变量,内容为content值 ( "overflow")。
scanner := bufio.NewScanner(file)
for scanner.Scan() {
data := scanner.Text()
data_arr := strings.Split(data, "|")
for _, n := range data_arr {
data_subdoc := strings.Split(n, ":")
a, b := data_subdoc[0], data_subdoc[1]
fmt.Println(a, b)
但问题是我得到的数据是(标题、内容、元数据和每行评论之间的关系丢失)
title stack
content overflow
metadata 53
comments none
title google
content website
metadata 213
comments Demos
但是,我想要这样的东西:
stack overflow 53
if stack has 53:
print comments (in this case, its 'none')
google website 213
if google has 213, print content (In this case, its 'website')
收到一只叮咚
相关分类