- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
下面的代码从这个文件中读取它的值:
2 3\n
1.0 2.0 3.0\n
-1.0 -2.0 -3.0\n
并且应该打印:[{1 2 3},{-1 -2 -3}]
但我得到的是:
[{2 [31 2 3]} {0 []}] strconv.ParseFloat:解析“3.0-1.0”:语法无效
reader.ReadLine()
似乎停留在同一位置。有没有更简单的方法来扫描行,然后扫描每行内的值?
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"strconv"
"strings"
)
type Example struct {
classLabel int
attributes []float64
}
func NewExample(classLabel int, attributes []float64) *Example {
return &Example{classLabel, attributes}
}
func readFile(path string) ([]Example, error) {
var (
result []Example
err error
file *os.File
part []byte
size int
attributeNum int
)
if file, err = os.Open(path); err != nil {
return result, err
}
defer file.Close()
reader := bufio.NewReader(file)
buffer := bytes.NewBuffer(make([]byte, 0))
if part, _, err = reader.ReadLine(); err != nil {
return result, err
}
buffer.Write(part)
newLine := buffer.String()
fmt.Println("newLine=" + newLine)
r := strings.NewReader(newLine)
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanWords)
if scanner.Scan() {
size, err = strconv.Atoi(scanner.Text())
if err != nil {
return result, err
}
}
fmt.Println("size=" + strconv.Itoa(size))
if scanner.Scan() {
attributeNum, err = strconv.Atoi(scanner.Text())
if err != nil {
return result, err
}
}
fmt.Println("attributeNum=" + strconv.Itoa(attributeNum))
result = make([]Example, size)
var classLabel int
var attributes []float64
for k := 0; k < size; k++ {
if part, _, err = reader.ReadLine(); err != nil {
return result, err
}
buffer.Write(part)
newLine := buffer.String()
fmt.Println("newLine=" + newLine)
r := strings.NewReader(newLine)
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanWords)
if scanner.Scan() {
classLabel, err = strconv.Atoi(scanner.Text())
if err != nil {
return result, err
}
}
fmt.Println("classLabel=" + strconv.Itoa(classLabel))
for i := 0; i < attributeNum; i++ {
var attribute float64
if scanner.Scan() {
attribute, err = strconv.ParseFloat(scanner.Text(), 64)
if err != nil {
return result, err
}
attributes = append(attributes, attribute)
fmt.Println("attribute=" + strconv.FormatFloat(attribute, 'f', -1, 64))
}
}
result[k] = *NewExample(classLabel, attributes)
}
return result, scanner.Err()
}
func main() {
example, err := readFile("test.txt")
fmt.Println(example, err)
}
最佳答案
当您在 for
循环中执行此操作时:
buffer.Write(part)
newLine := buffer.String()
fmt.Println("newLine=" + newLine)
下一行被附加到buffer
。那是,在循环开始之前,buffer
包含 2 3
,然后在读取1.0 2.0 3.0
之后,它被附加到缓冲区,所以内容变成了2 31.0 2.0 3.0
,您将其存储在 newLine
中。这就是事情开始发生变化的地方。
您可能希望在读取每一行之前清除缓冲区
:
buffer.Reset()
buffer.Write(part)
newLine := buffer.String()
fmt.Println("newLine=" + newLine)
但是你还会有更多的问题,在这里:
if scanner.Scan() {
classLabel, err = strconv.Atoi(scanner.Text())
if err != nil {
return result, err
}
}
由于该行包含 1.0 2.0 3.0
,strconf.Atoi
将失败。我不明白这个片段的目的,也许您可以删除它(或注释掉)。
在解决了上面的问题之后,您仍然会遇到一个问题,在这一行:
attributes = append(attributes, attribute)
由于 attributes
永远不会重置,因此它会不断增长。也就是说,在第一行之后,它将包含 1 2 3
,在第二行之后它将包含 1 2 3 -1 -2 -3
。您可以通过在没有外循环的情况下移动 attributes
的声明来更正该问题,如下所示:
var attributes []float64
for i := 0; i < attributeNum; i++ {
var attribute float64
if scanner.Scan() {
attribute, err = strconv.ParseFloat(scanner.Text(), 64)
if err != nil {
return result, err
}
attributes = append(attributes, attribute)
fmt.Println("attribute=" + strconv.FormatFloat(attribute, 'f', -1, 64))
}
}
关于go - reader.ReadLine() 在 scanner.Scan() 调用后不前进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37205971/
class ReadLock { private: std::mutex readWriteMutex; std::mutex conditionmtx; std::condi
我在 utf-8 编码文件中有多个 messages.properties 文件(messages_en_US.properties、messages_fr.properties,...)。在这些属性
我正在尝试从 google reader api 中检索单个选定项目。是否可以通过 API 调用通过 ID 获取项目,或者我是否必须访问该项目提要并从那里获取它? 最佳答案 您可以使用 POST 到
好的,所以我有一个应用程序可以与 GR 的“api”紧密结合。 一切正常,但最近我收到来自 Google 的许可被拒绝返回。如果我退出 GR 并使用我的应用程序重新登录,一切都会重新开始。这让我相信
我想要的是最终得到类似的东西: public class InterleavedBufferedReader extends BufferedReader { ... } 并将其用作: Reader[
reader monad 有一个asks 函数,它的定义与reader 函数完全相同,为什么它作为一个单独的函数存在,与 的定义相同读者?为什么不总是使用阅读器? class Monad m => M
当使用csv模块读取文件时,有两种方法可以遍历csv.reader返回的生成器。 with open('foo.csv') as f: reader = csv.reader(f) r
我想在 Go 中按照 here 中的要求做同样的事情. 我正在解析一个巨大的日志文件,我需要逐行解析它。在每一行上,我将该行反序列化为一个结构。数据可能来自任何数据源(文件、网络等)。因此,我在我的函
我在golang的zlib/reader.go文件中找到了很多像r.(flate.Reader)这样的代码片段。这是什么意思? https://golang.org/src/compress/zlib
我正在 Spring MVC 中包装 Freemarker 模板加载器,如所述 here在 html 页面中进行默认转义。 所以,我需要用我的字符串包装来自 java.io.Reader 的内容,而不
为什么这个 PDF 在 Foxit Reader 而不是 Adobe Reader 中显示签名? 这是来自 Syncfusion PDF library 的代码用于生成它(另请参阅有关 signi
我有一个巨大的tbb::concurrent_unordered_map被多个(~60)线程同时“大量读取”。 我每天需要清除一次(完全清除或选择性清除)。在 tbb 中删除显然不是线程安全的实现,因
好像是 Hibernate.createClob(Reader reader, int length)在 3.6.x 版本中已弃用 它建议使用使用 LobHelper.createClob(Reade
这是我的实际解决方案 private def transpose[E, A](readers : Seq[Reader[E, A]]) : Reader[E, Seq[A]] = Read
DataReader[0].ToString() 和 (string)DataReader[0] 有区别吗? 我的猜测是,如果数据库类型不是字符串类型,(string)DataReader[0] 可能
我想制作一个 C# 程序来保存 pdf 和 djvu 文件的书签。如何从 AcroRd32/DjVuReader 进程中找出当前页码? 最佳答案 您可以通过 Adobe Acrobat 支持的 D
什么更好 var s = (string)reader[0] 或 var s = Convert.ToString(reader[0]) ? 最佳答案 我会说 reader.GetString(0
我对非官方阅读器 api 进行了大量研究,并筛选了其他问题,但没有一个完全满足我的要求。如果您知道文章 id 有据可查,如何分享文章,但如果您不知道 id,我想知道如何分享文章(即如何转换 url -
这是一个简单的示例,用于将 xml 文件读入 WebRowSet 对象,然后将数据从该对象加载到数据库。 import javax.sql.rowset.RowSetProvider; import
这样的转换对于任何仿函数都是可能的,不仅是Future: implicit class RichFunctorReader[F[_]: Functor, A, B](fr: F[Reader[A, B
我是一名优秀的程序员,十分优秀!