我正在尝试编写一个更改CSV文件列顺序的程序。 我使用以下代码
公开 共享 Sub ReadCSV() Dim columnstoselect As 字符串() Dim csvFile As 字符串 = D:\Projects \\ \\ _MQA_Remap_CSV \Input.csv Dim lines = File.ReadAllLines(csvFile) Dim outFile 作为 字符串 = D:\Projects\MQA_Remap_CSV \ Output.csv Dim data As 字符串() Dim 格式作为 字符串 = CReadConfig.ReadConfigSource( D:\Projects\MQA_Remap_CSV \ config.csv) Form1.TextBox1.Text = CReadConfig.ReadConfigtarget( D:\Projects\MQA_Remap_CSV \ config.csv)。ToString 使用 fs 作为 新 StreamWriter(outFile, False ) 对于 每个 s 作为 字符串 在行中 data = s .Split( , c) fs.WriteLine( String .Format(format,CReadConfig.ReadConfigtarget( D:\\ \\ PROjects\MQA_Remap_CSV \ config.csv)。ToArray )) 下一步 结束 使用我有这个功能的问题
CReadConfig.ReadConfigtarget( D:\Projects\MQA_Remap_CSV \ config.csv)函数l这样的事情
公共 共享 功能 ReadConfigtarget(filePath As 字符串)作为列表( 字符串) ' 将目标或数组行的值作为数组返回 Dim str As String () Dim lines = File.ReadAllLines(filePath).Skip( 1 ) Dim val As String Dim i A s 字符串 = 0 使用 sr 作为 新 StreamReader(filePath) Dim out As 新列表( 字符串) For 每个 s 作为 字符串 在行中 str = s.Split( , c) val =修剪( data(& ; (str( 1 ) - 1 )& ))。ToString out.Add(val) 下一步 返回 out 结束 使用 结束 函数这个函数读取一个csv文件,应该返回一个看起来像这样的值列表 数据(0),数据(1) 因此如果我替换此行有效
fs.WriteLine( String .Format(format,CReadConfig.ReadConfigtarget( D:\Projects\MQA_Remap_CSV \ config.csv)。ToArraywith
fs.WriteLine( String 。格式(格式,数据( 0 ),数据( 1 ),数据( 2 ))代码工作并写出数据中的数据( 0),数据(1)等,但是当我使用该函数时,它将它作为文字字符串并在csv文件中写出文本'data(0)',data(1)。 关于如何将函数值作为参数而不是文字文本传递的任何想法?
解决方案如果它正在编写变量名称作为输出,你可能做的是: fs.WriteLine( String .Format(格式, data(0),data(1),data(2)))
或
fs.WriteLine( String .Format( {0},{1},{2},data(0),data(1),dat a(2)))相反。 但是Carlo是对的: fs.WriteLine( String .Format(format,CReadConfig.ReadConfigtarget( D:\Projects\MQA_Remap_CSV \ config.csv)。ToArray()))
将完美运行。
I'm trying to write a program which changes the column order of a CSV file. Im using the following code
Public Shared Sub ReadCSV() Dim columnstoselect As String() Dim csvFile As String = "D:\Projects\MQA_Remap_CSV\Input.csv" Dim lines = File.ReadAllLines(csvFile) Dim outFile As String = "D:\Projects\MQA_Remap_CSV\Output.csv" Dim data As String() Dim format As String = CReadConfig.ReadConfigSource("D:\Projects\MQA_Remap_CSV\config.csv") Form1.TextBox1.Text = CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv").ToString Using fs As New StreamWriter(outFile, False) For Each s As String In lines data = s.Split(","c) fs.WriteLine(String.Format(format, CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv").ToArray ) ) Next End UsingI am having a problem with this function
CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv")the function looks like this
Public Shared Function ReadConfigtarget(filePath As String) As List(Of String) 'returns the Value of target or array rows as an array Dim str As String() Dim lines = File.ReadAllLines(filePath).Skip(1) Dim val As String Dim i As String = 0 Using sr As New StreamReader(filePath) Dim out As New List(Of String) For Each s As String In lines str = s.Split(","c) val = Trim("data(" & (str(1) - 1) & ") ").ToString out.Add(val) Next Return out End Using End Functionthis function reads a csv file and should return a list of values which look like this data(0), data(1) so in effect if i replace this line
fs.WriteLine(String.Format(format, CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv").ToArraywith
fs.WriteLine(String.Format(format, data(0), data(1), data(2))the code works and writes out the data in data(0) ,data(1) and so on , however when i use the function it is taking it as a literal string and writing out text 'data(0)' , data(1) in the csv file. Any ideas on how to pass the function values as argument and not literal text?
解决方案 If it's writing the variable names as the output then what you've probably done is: fs.WriteLine(String.Format(format, "data(0), data(1), data(2)"))Or
fs.WriteLine(String.Format("{0},{1},{2}, data(0), data(1), data(2)"))Instead. But Carlo is right:
fs.WriteLine(String.Format(format, CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv").ToArray()))will work perfectly.
更多推荐
VB.Net传递字符串列表作为字符串.format的参数
发布评论