Golang文件读取只读取最后一行

Golang file reading only reading last line

所以我拿了一些公开的数据,看起来像这样 -

这是文件

http://expirebox.com/download/b149b744768fb11aee9c5e26ad409bcc.html

,,,% of Total Expenditure,,,
Function Code,Type of Activity,Expenditure,Dollars/Student (ADA),"This District (ADA 49,497)",All Unified School Districts,Statewide Average
1000-1999ÊÊ,INSTRUCTIONÊÊ,"9,397,226",",039",42%,62%,62%
1000,Instruction,"7,472,790ÊÊ",",000",42%,48%,49%
1110,Special Education: Separate Classes,",004,074",,N/A,N/A,N/A
1120,Special Education: Resource Specialist Instruction,"1,629",,N/A,N/A,N/A
1130,Special Education: Supplemental Aids & Services in Regular Classrooms,",747",,N/A,N/A,N/A
1180,Special Education:  Nonpublic Agencies/Schools (NPA/S),N/A,N/A,N/A,N/A,N/A
1190,Special Education:  Other Specialized Instructional Services,",985",,N/A,N/A,N/A
1100-1199,Instruction - Special Education,",924,436ÊÊ",,0%,14%,13%
"Subtotal, INSTRUCTION",,"9,397,226",",039",42%,62%,62%
2000-2999ÊÊ,INSTRUCTION-RELATED SERVICESÊÊ,"2,783,414",",683",22%,12%,12%
2100,Instructional Supervision and Administration,",551,041",",809",N/A,N/A,N/A
2110,Instructional Supervision,N/A,N/A,N/A,N/A,N/A
2120,Instructional Research,N/A,N/A,N/A,N/A,N/A
2130,Curriculum Development,"8,369",,N/A,N/A,N/A
2140,In-house Instructional Staff Development,",855",[=10=],N/A,N/A,N/A
2150,Instructional Administration of Special Projects,N/A,N/A,N/A,N/A,N/A
2100-2199,Instructional Supervision and Administration,",919,265ÊÊ",",817",15%,4%,4%
2200,Administrative Unit (AU) of a Multidistrict SELPA,[=10=],[=10=],0%,0%,0%
2420,"Instructional Library, Media, and Technology",",295,033ÊÊ",8,1%,1%,1%
2490,Other Instructional Resources,"8,734",,N/A,N/A,N/A
2495,Parent Participation,",830",,N/A,N/A,N/A
2490-2495,Other Instructional Resources,"6,565ÊÊ",,0%,1%,0%
2700,School Administration,",932,551ÊÊ",6,6%,7%,7%
"Subtotal, INSTRUCTION-RELATED SERVICES",,"2,783,414",",683",22%,12%,12%
3000-3999ÊÊ,PUPIL SERVICESÊÊ,",325,938",6,8%,8%,8%
4000-4999ÊÊ,ANCILLARY SERVICESÊÊ,",207,263",,0%,1%,1%
5000-5999ÊÊ,COMMUNITY SERVICESÊÊ,[=10=],[=10=],0%,0%,0%
6000-6999ÊÊ,ENTERPRISEÊÊ,",264",[=10=],0%,0%,0%
7000-7999ÊÊ,GENERAL ADMINISTRATIONÊÊ,",916,858",4,5%,5%,6%
8000-8999ÊÊ,PLANT SERVICESÊÊ,",172,247",",115",9%,11%,10%
9000-9999ÊÊ,OTHER OUTGOÊÊ,",981,716",N/A,14%,2%,2%
"Total Expenditures, All Activities",,"4,788,926",",017",100%,100%,100%

它在 csv 中。

我试过这个代码

file, err := os.Open("expenses.csv")
if err != nil {
    log.Fatal(err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
    fmt.Println(scanner.Text())
}

if err := scanner.Err(); err != nil {
    log.Fatal(err)
}

还有这个

content, err := ioutil.ReadFile("expenses.csv")

lines := strings.Split(string(content), "\n")

fmt.Println(lines)

check(err)

dat, err := os.Open("expenses.csv")
check(err)

defer dat.Close()

reader := csv.NewReader(dat)
reader.LazyQuotes = true

reader.FieldsPerRecord = -1

rawCSVData, err := reader.ReadAll()

check(err)
fmt.Println(rawCSVData)

for _, each := range rawCSVData {
    fmt.Println(each)
}

支票在哪里

func check(e error) {
    if e != nil {
        panic(e)
    }
}

在这两种情况下我都得到了这个结果 -

"Total Expenditures, All Activities",,"4,788,926",",017",100%,100%,100%,1%15%,4%,4%AA,N/A,N/Anified School Districts,Statewide Average

而不是所有的行。

为什么我只读到最后一行?

基本问题是该文件有 \r 行结尾。它也不是有效的 UTF-8。这些加起来会造成 Scanner 很多麻烦。

首先,我们可以使用 xxd

查看文件中的确切内容
00000000: 2c2c 2c25 206f 6620 546f 7461 6c20 4578  ,,,% of Total Ex
00000010: 7065 6e64 6974 7572 652c 2c2c 0d46 756e  penditure,,,.Fun

如果你看,你会看到行尾是 0d,也就是 \rScanner 需要它是 \r\n\n.

接下来,您可能 运行 遇到麻烦,因为它不是 UTF-8。所有那些 Ê 实际上都是 0xCA,这不是有效的 UTF-8 编码。我们可以再次在 xxd 中看到:

000000b0: 3939 39ca ca2c 494e 5354 5255 4354 494f  999..,INSTRUCTIO
000000c0: 4eca ca2c 2224 3234 392c 3339 372c 3232  N..,"9,397,22

Go 可能只是将它作为字节一起发送(并得到 Ê),这是许多编辑器尝试做的事情,但它可能会造成麻烦。

如果可能,请重新格式化此文件以使用 Unix 或 Windows UTF-8 行结尾。