读取 CSV 文件并将值存储到数组中
我正在尝试读取 *.csv
文件。
*.csv
文件包含以分号(“;”)分隔的两列。
我能够使用 StreamReader 读取 *.csv
文件,并能够使用 Split()
函数分隔每一行。我想将每一列存储到一个单独的数组中,然后显示它。
可以这样做吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
你可以这样做:
You can do it like this:
我最喜欢的 CSV 解析器是内置于 .NET 库中的解析器。这是 Microsoft.VisualBasic 命名空间中隐藏的宝藏。
下面是示例代码:
请记住添加对
Microsoft.VisualBasic
的引用这里给出了有关解析器的更多详细信息:http://codeskaters.blogspot.ae/2015/11/c-easiest-csv-parser-built-in-net.html
My favourite CSV parser is one built into .NET library. This is a hidden treasure inside Microsoft.VisualBasic namespace.
Below is a sample code:
Remember to add reference to
Microsoft.VisualBasic
More details about the parser is given here: http://codeskaters.blogspot.ae/2015/11/c-easiest-csv-parser-built-in-net.html
LINQ 方式:
^^错误 - 由 Nick 编辑
看来原始回答者正在尝试使用二维数组(包含数组的数组)填充
csv
。第一个数组中的每个项目都包含一个表示该行号的数组,而嵌套数组中的每个项目都包含该特定列的数据。LINQ way:
^^Wrong - Edit by Nick
It appears the original answerer was attempting to populate
csv
with a 2 dimensional array - an array containing arrays. Each item in the first array contains an array representing that line number with each item in the nested array containing the data for that specific column.刚刚遇到这个库: https://github.com/JoshClose/CsvHelper
非常直观且易于使用使用。也有一个 nuget 包,可以快速实现: https://www.nuget.org/packages/ CsvHelper。似乎也得到了我喜欢的积极维护。
将其配置为使用分号很简单: https://github.com/JoshClose /CsvHelper/wiki/自定义配置
Just came across this library: https://github.com/JoshClose/CsvHelper
Very intuitive and easy to use. Has a nuget package too which made is quick to implement: https://www.nuget.org/packages/CsvHelper. Also appears to be actively maintained which I like.
Configuring it to use a semi-colon is easy: https://github.com/JoshClose/CsvHelper/wiki/Custom-Configurations
您无法立即创建数组,因为您需要从头开始知道行数(这需要读取 csv 文件两次)
您可以将值存储在两个
List
和然后使用它们或使用List.ToArray()
转换为数组非常简单的示例:
注意
,这只是一个非常简单的示例。使用
string.Split
无法解决某些记录内部包含分隔符;
的情况。为了更安全的方法,请考虑使用一些 csv 特定的库,例如 nuget 上的 CsvHelper。
You can't create an array immediately because you need to know the number of rows from the beginning (and this would require to read the csv file twice)
You can store values in two
List<T>
and then use them or convert into an array usingList<T>.ToArray()
Very simple example:
N.B.
Please note that this is just a very simple example. Using
string.Split
does not account for cases where some records contain the separator;
inside it.For a safer approach, consider using some csv specific libraries like CsvHelper on nuget.
我通常使用这个来自codeproject的解析器,因为它为我处理了一堆字符转义和类似的情况。
I usually use this parser from codeproject, since there's a bunch of character escapes and similar that it handles for me.
这是我对投票最高的答案的变体:
然后可以使用
csv
变量,如下例所示:Here is my variation of the top voted answer:
The
csv
variable can then be used as in the following example:如果您需要跳过(标题)行和/或列,您可以使用它来创建一个二维数组:
如果您需要在进一步处理数据之前对数据进行整形(假设前两行包含标题的第一列是行标题 - 您不需要将其包含在数组中,因为您只想查看数据)。
注意 您可以使用以下代码轻松获取标题和第一列:
此代码示例假定您的
*.csv
文件具有以下结构:注意: 如果您需要跳过空行 - 有时可以很方便,您可以通过插入来完成
上面 LINQ 代码示例中的
from
和select
语句之间。If you need to skip (head-)lines and/or columns, you can use this to create a 2-dimensional array:
This is quite useful if you need to shape the data before you process it further (assuming the first 2 lines consist of the headline, and the first column is a row title - which you don't need to have in the array because you just want to regard the data).
N.B. You can easily get the headlines and the 1st column by using the following code:
This code example assumes the following structure of your
*.csv
file:Note: If you need to skip empty rows - which can by handy sometimes, you can do so by inserting
between the
from
and theselect
statement in the LINQ code examples above.您可以在 C# 中使用 Microsoft.VisualBasic.FileIO.TextFieldParser dll 以获得更好的性能,
请获取上面文章中的以下代码示例
You can use Microsoft.VisualBasic.FileIO.TextFieldParser dll in C# for better performance
get below code example from above article
我花了几个小时寻找合适的库,但最后我编写了自己的代码:)
您可以使用任何您想要的工具读取文件(或数据库),然后将以下例程应用于每一行:
I have spend few hours searching for a right library, but finally I wrote my own code :)
You can read file (or database) with whatever tools you want and then apply the following routine to each line:
大家好,我创建了一个静态类来执行此操作。
+ 列检查
+ 配额符号删除
Hi all, I created a static class for doing this.
+ column check
+ quota sign removal
这是一种特殊情况,其中一个数据字段将分号(“;”)作为其数据的一部分,在这种情况下,上面的大多数答案都会失败。
在这种情况下的解决方案将是
Here's a special case where one of data field has semicolon (";") as part of it's data in that case most of answers above will fail.
Solution in that case will be
开源 Angara.Table 库允许将 CSV 加载到类型列中,因此您可以从列中获取数组。每列都可以按名称或索引进行索引。请参阅http://predictionmachines.github.io/Angara.Table/saveload.html。
该库遵循 CSV 的 RFC4180;它支持类型推断和多行字符串。
示例:
您可以使用 Column 类型查看列类型,例如,
由于该库专注于 F#,因此您可能需要添加对 FSharp.Core 4.4 程序集的引用;单击项目上的“添加引用”,然后在“Assemblies”下选择 FSharp.Core 4.4 -> “扩展”。
The open-source Angara.Table library allows to load CSV into typed columns, so you can get the arrays from the columns. Each column can be indexed both by name or index. See http://predictionmachines.github.io/Angara.Table/saveload.html.
The library follows RFC4180 for CSV; it enables type inference and multiline strings.
Example:
You can see a column type using the type Column, e.g.
Since the library is focused on F#, you might need to add a reference to the FSharp.Core 4.4 assembly; click 'Add Reference' on the project and choose FSharp.Core 4.4 under "Assemblies" -> "Extensions".
我已经使用 csvreader.com(付费组件)很多年了,从来没有遇到过问题。它坚固、小巧、速度快,但你必须为此付费。您可以将分隔符设置为您喜欢的任何内容。
I have been using csvreader.com(paid component) for years, and I have never had a problem. It is solid, small and fast, but you do have to pay for it. You can set the delimiter to whatever you like.
我只是一个正在写硕士论文的学生,但这就是我解决问题的方法,而且对我来说效果很好。首先,从目录中选择文件(仅限 csv 格式),然后将数据放入列表中。
I am just student working on my master's thesis, but this is the way I solved it and it worked well for me. First you select your file from directory (only in csv format) and then you put the data into the lists.
这是我的 2 个简单的静态方法,用于将文本从 csv 文件转换为
List
,反之亦然。每种方法都使用行转换器。>
此代码应考虑 csv 文件的所有可能性。您可以定义自己的csv分隔符,此方法尝试纠正转义双“引号”字符,并处理引号中的所有文本都是一个单元格且的情况csv 分隔符位于带引号的字符串内,包括一个单元格中的多行,并且可以忽略空行。
最后一种方法仅用于测试。因此您可以忽略它,或者使用此测试方法测试您自己的或其他解决方案:)。为了进行测试,我使用了 4 行 2 行的硬 csv:
这是最终代码。为了简单起见,我删除了所有 try catch 块。
使用示例:
注释:
此:
char tempQuote = (char)162;
是 ASCI 表中的第一个罕见字符。该脚本会搜索该字符或文本中不存在的前几个 ascii 字符,并将其用作临时转义和引号字符。This is my 2 simple static methods to convert text from csv file to
List<List<string>>
and vice versa. Each method use row convertor.This code should take into account all the possibilities of the csv file. You can define own csv separator and this methods try to correct escape double 'quote' char, and deals with the situation when all text in quotes are one cell and csv separator is inside quoted string including multiple lines in one cell and can ignore empty rows.
Last method is only for testing. So you can ignore it, or test your own, or others solution with this test method :). For testing I used this hard csv with 2 rows on 4 lines:
This is final code. For simplicity, I removed all try catch blocks.
Usage examples:
Notes:
This:
char tempQuote = (char)162;
is first rare character from ASCI table. The script searches for this, or the first next few ascii character that is NOT in the text and uses it as a temporary escape and quote characters.还是错了。您需要补偿引号中的“”。
这是我的解决方案 Microsoft 风格 csv。
Still wrong. You need to compensate for "" in quotes.
Here is my solution Microsoft style csv.
我有一个图书馆可以满足您的需求。
不久前,我编写了简单且足够快速的库来处理 CSV 文件。您可以通过以下链接找到它: https://github.com/ukushu /DataExporter/blob/master/Csv.cs
它与 CSV 一起使用,就像二维数组一样。正是你所需要的。
例如,如果您需要第三行的所有值,则只需编写:
或读取第三行的第二个单元格:
I have a library that is doing exactly you need.
Some time ago I had wrote simple and fast enough library for work with CSV files. You can find it by the following link: https://github.com/ukushu/DataExporter/blob/master/Csv.cs
It works with CSV like with 2 dimensions array. Exactly like you need.
As example, in case of you need all of values of 3rd row only you need is to write:
or to read 2nd cell of 3rd row:
以下代码中的 csv 中需要标头才能进行 json 转换
您可以按原样使用以下代码,无需进行任何更改。
此代码适用于两个行标题或一个行标题。
下面的代码读取上传的 IForm 文件并转换为内存流。
如果您想使用文件路径而不是上传的文件,可以将
new StreamReader(ms, System.Text.Encoding.UTF8, true)) 替换为 new StreamReader("../../examplefilepath");
Headers are required in csv for json conversion in the below code
You can use below code as is without making any changes.
This code will work with two row headers or with one row header.
Below code reads the uploaded IForm File and converts to memory stream.
If you want to use file path instead of uploaded file you can replace
new StreamReader(ms, System.Text.Encoding.UTF8, true)) with new StreamReader("../../examplefilepath");
下面的代码行用于读取 csv 文件数据。
Below line of code is for reading csv file data.
看看这个
使用 CsvFramework
;使用 System.Collections.Generic;
命名空间 CvsParser
{
}
look at this
using CsvFramework;
using System.Collections.Generic;
namespace CvsParser
{
}