草庐IT

string - 戈朗 : read text file line by line of int strings

coder 2023-06-29 原文

我正在处理一个包含整数列表作为字符串的输入文件

10
..

我选择使用 ReadString('\n') 逐行阅读它方法

下面的代码

line, error := inputReader.ReadString('\n')
lineStr := string(line)

控制台输出(长度和值)

lineStr %v 4
lineStr %v 10

lineStr 的长度为“4”,可能是因为 rune 编码。

然后我尝试了几种方法将其转换为简单整数但没有成功。

Ex1

num, _ := strconv.ParseUint(lineStr, 0, 64)

输出一个数字0(应该是10)

Ex2

num, _ := strconv.Atoi(lineStr)

输出一个数字0(应该是10)

Ex3

num, _ := strconv.Atoi("10")

输出数字 10(ok)

Ex4

num, _ := strconv.ParseUint("10", 0, 64)

输出数字 10(ok)

文字中的字符串可以,但文件中的字符串不起作用,有什么问题吗?

提前致谢

最佳答案

来自文档:“ReadString 一直读取到输入中第一次出现分隔符,返回一个包含数据的字符串,直至并包括分隔符。”

因此,我建议您使用扫描仪,它可以像您期望的那样处理这种情况:

scanner := bufio.NewScanner(file)
for scanner.Scan() {
    lineStr := scanner.Text()
    num, _ := strconv.Atoi(lineStr)
    fmt.Println(lineStr, num)
}

关于string - 戈朗 : read text file line by line of int strings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28983831/

有关string - 戈朗 : read text file line by line of int strings的更多相关文章

  1. ruby - 字符串文字中的转义状态作为 `String#tr` 的参数 - 2

    对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一

  2. ruby - 从 String#split 返回的零长度字符串 - 2

    在Ruby1.9.3(可能还有更早的版本,不确定)中,我试图弄清楚为什么Ruby的String#split方法会给我某些结果。我得到的结果似乎与我的预期相反。这是一个例子:"abcabc".split("b")#=>["a","ca","c"]"abcabc".split("a")#=>["","bc","bc"]"abcabc".split("c")#=>["ab","ab"]在这里,第一个示例返回的正是我所期望的。但在第二个示例中,我很困惑为什么#split返回零长度字符串作为返回数组的第一个值。这是什么原因呢?这是我所期望的:"abcabc".split("a")#=>["bc"

  3. ruby - json 没有将 String 隐式转换为 Integer (TypeError) - 2

    玩转ruby​​,我已经:#!/usr/bin/ruby-w#WorldweatheronlineAPIurlformat:http://api.worldweatheronline.com/free/v1/weather.ashx?q={location}&format=json&num_of_days=1&date=today&key={api_key}require'net/http'require'json'@api_key='xxx'@location='city'@url="http://api.worldweatheronline.com/free/v1/weather.

  4. ruby - 类型错误 : can't convert String into Integer - 2

    我有代码:classScenedefinitialize(number)@number=numberendattr_reader:numberendscenes=[Scene.new("one"),Scene.new("one"),Scene.new("two"),Scene.new("one")]groups=scenes.inject({})do|new_hash,scene|new_hash[scene.number]=[]ifnew_hash[scene.number].nil?new_hash[scene.number]当我启动它时出现错误:freq.rb:11:in`[]'

  5. ruby - 为什么 String::sub!() 会更改 Ruby 中克隆对象的原始对象? - 2

    我的Ruby代码中有一个看起来有点像这样的结构Parameter=Struct.new(:name,:id,:default_value,:minimum,:maximum)稍后,我使用创建了这个结构的一个实例freq=Parameter.new('frequency',15,1000.0,20.0,20000.0)在某些时候,我需要这个结构的精确副本,所以我调用newFreq=freq.clone然后,我更改newFreq的名称newFreq.name.sub!('f','newF')奇迹般地,它也改变了freq.name!像newFreq.name='newFrequency'这样

  6. ruby - 重构 Ruby : Converting string array to int array - 2

    我正在重构一个西洋跳棋程序,我正在尝试将玩家移动请求(例如以“3、3、5、5”的形式)处理到一个int数组中。我有以下方法,但感觉不像我所知道的那样像Ruby:deftranslate_move_request_to_coordinates(move_request)return_array=[]coords_array=move_request.chomp.split(',')coords_array.each_with_indexdo|i,x|return_array[x]=i.to_iendreturn_arrayend我用它进行了以下RSpec测试。it"translatesa

  7. ruby - "string literal in condition"是什么意思? - 2

    每当我尝试运行该程序时,都会弹出一条错误消息“条件字符串文字(第10行)”。我做错了什么?puts"Welcometothebestcalculatorthereis.Wouldyouliketo(a)calculatetheareaofageometricshapeor(b)calculatetheequationofaparabola?Pleaseenteran'a'ora'b'togetstarted."response=gets.chompifresponse=="a"or"A"puts"ok."elsifresponse=="b"or"B"puts"awesome."else

  8. ruby - 杰基尔服务错误 : no implicit conversion of nil into String - 2

    我用这个错误搜索了jekyll。jekyll处理页面时似乎出现了ruby​​错误,但我根本不了解ruby​​。杰基尔版本1.3.1我什至重新安装了ruby​​和jekyll,但结果没有改变。更新:在我将jekyll从1.31降级到1.20后,这个错误消失了注意:我的网站是用jekyll1.20创建的,所以它不能用1.3.1构建?这是核心问题吗?E:\GitHub\sample>jekyll服务--trace:Configurationfile:E:/GitHub/sample/_config.ymlSource:E:/GitHub/sampleDestination:E:/GitHub

  9. ruby - 为什么 `Symbol#match` 的行为与 `String#match` 和 `Regexp#match` 不同? - 2

    String#match和Regexp#match在匹配成功时返回一个MatchData:"".match(//)#=>#//.match("")#=>#//.match(:"")#=>#但是Symbol#match返回匹配位置(如String#=~)::"".match(//)#=>0为什么Symbol#match表现不同?有用例吗? 最佳答案 我将其报告为Ruby核心中的错误:https://bugs.ruby-lang.org/issues/11991.让我们看看他们会怎么说。更新被质疑的行为似乎是一个错误。似乎从Ruby2.

  10. ruby-on-rails - 类型错误 : wrong argument type String (expected Module) - 2

    我有以下代码:classProfileLookup基本上包含大量查找数据,按类别拆分。目的是为数据库中的每个类别创建一个方法。通过Rails控制台,此代码按预期工作:ruby-1.9.3@hub:002>ProfileLookup.available_gendersProfileLookupLoad(0.6ms)SELECT"profile_lookups".*FROM"profile_lookups"WHERE"profile_lookups"."category"='gender'ORDERBYvalue=>["Female","Male"]但是,我的规范不合格。以下规范:requ

随机推荐