Vb.net |从.txt文件导入数据并将其存储在数组中 [英] Vb.net | import data from a .txt file and store them in an array
问题描述
大家好,我是VB.NET的新手,我需要你对我的问题的帮助。我认为解决它并不困难,但现在我已经卡住了。
所以,我有一个.txt文件,我们称之为data.txt。这个文件有一个数组(22x2),数字是
(例如0.04 0.2587676)。
数据表
我需要加载这个文件并将其存储到一个数组中,然后我必须用数字取数字,然后用一个公式计算,然后把它存放到一个新的数组。
谢谢你的时间
Iosif
我的尝试:
Hello to all, I am new at VB.NET and I need your help about my problem. I think its not so hard to solve it, but right now I have stuck.
So, I have a .txt file, lets call it data.txt . This file has a array (22x2) with numbers
(e.g 0.04 0.2587676).
DATA TABLE
I need to load this file and store it to an array, and then I have to take its number by number and make some calculations with one formula and then to store it to a new array.
Thank you for you time
Iosif
What I have tried:
Dim strFileName() As String '// String Array.
Dim scalings As String = "" '// temp String for result.
strFileName = IO.File.ReadAllLines("C:\Users\x-ios\Documents\#Projects_IVCV_MOS\P120N22.txt") '// add each line as String Array.
For Each myLine In strFileName '// loop thru Arrays.
scalings &= myLine & vbNewLine '// add Array and new line.\
Next
推荐答案
参见使用OleDb导入文本文件(选项卡,CSV,自定义) ) [ ^ ]。
请注意 ReadAllLines [ ^ ]返回一个包含文件所有行的字符串数组。您现在需要做的就是拆分 [ ^ ]数据[ ] (空间)。如果有两列数字用空格分隔,则可以使用 Linq [ ^ ]实现这一目标,即:
Note that ReadAllLines[^] returns a string array containing all lines of the file. All you need to do right now is to Split[^] data on [" "] (space). If there are two columns of numbers separated by space, you can use Linq[^] to achieve that, i.e.:
Dim strFileName = IO.File.ReadAllLines("D:\gVcBp4kv.txt")
Dim cu As Globalization.CultureInfo = Globalization.CultureInfo.CreateSpecificCulture("en-US")
Dim style As Globalization.NumberStyles = Globalization.NumberStyles.Number Or Globalization.NumberStyles.AllowCurrencySymbol
Dim arrData = strFileName _
.Select(Function(x) New With _
{
.FirstCol = Double.Parse(x.Split(New String(){Microsoft.VisualBasic.vbTab}, StringSplitOptions.RemoveEmptyEntries)(0), style, cu), _
.SecondCol = Double.Parse(x.Split(New String(){Microsoft.VisualBasic.vbTab}, StringSplitOptions.RemoveEmptyEntries)(1), style, cu) _
}) _
.ToList()
'data are ready to use
For Each row In arrData
Console.WriteLine("{0}-{1}={2}", row.SecondCol, row.FirstCol, row.SecondCol -row.FirstCol)
Next
以上代码返回:
Above code returns:
0.4568666-0.05=0.4068666
0.57215975-0.06=0.51215975
0.69595725-0.07=0.62595725
0.809528-0.08=0.729528
...and so on...
现在,你是能够提供计算。
详情请见:
String.Split方法(String [],StringSplitOptions)(系统) [ ^ ]
Double.Parse Method(String,NumberStyles,IFormatProvider)(System) [ ^ ]
祝你好运!
Now, you're able to provide calculations.
For further details, please, see:
String.Split Method (String[], StringSplitOptions) (System)[^]
Double.Parse Method (String, NumberStyles, IFormatProvider) (System)[^]
Good luck!
当然我需要你的帮助。我的数据类似于 pastebin.com - gVcBp4kv.txt 。第二列用a分隔vbtab。我用一个代码运行它,你可以在那篇文章中看到如何将从.txt导入的字符串二维数组转换为VB.NET中的double [ ^ ]。但是,我无法将smatrix数组转换为新的double,因为后来我希望这些元素可以通过ABS函数1来测试。
Ofcourse I need your help. My data is like this pastebin.com - gVcBp4kv.txt .The 2nd column is separated with a vbtab .I run this with a code as you can see in that post How do I convert a string 2-dimension array imported from a .txt to a double in VB.NET[^] . But , I can't convert the smatrix array to a new double, because later I want these elements to be tested by ABS function 1 by 1.
根据您在pastebin.com服务上发布的数据,我已根据您的需要更改了我的代码。
Based on data you posted on pastebin.com service i've changed my code to your needs.
这篇关于Vb.net |从.txt文件导入数据并将其存储在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!