读取 CSV 数据的最佳方式是什么?

发布于 2024-07-09 15:30:59 字数 363 浏览 6 评论 0原文

我正在使用 C# 并尝试使用此连接字符串读取 CSV

Provider=Microsoft.Jet.OLEDB.4.0;数据源=C:\Documents and Settings\rajesh.yadava\Desktop\orcad;扩展属性=“Text;HDR=YES;IMEX=1;FMT=”分隔” 
  

这适用于制表符分隔的数据。

我想要一个连接字符串,它应该用于制表符分隔以及逗号(,)和管道(|)。

如何为 CSV 创建通用连接字符串。

谢谢 拉杰什

I am using C# and trying to read a CSV by using this connection string;

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\rajesh.yadava\Desktop\orcad;Extended  Properties="Text;HDR=YES;IMEX=1;FMT=Delimited"

This works for tab delimited data.

I want a connection string which should for tab delimited as well as comma(,) and pipe(|).

How can I make a generic connection string for CSV.

Thanks
Rajesh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

世俗缘 2024-07-16 15:30:59

filehelpers 库是一个选项吗?

Is the filehelpers library an option?

温柔戏命师 2024-07-16 15:30:59

我知道这并不能回答你的问题,但这里有一个警告。

我必须创建自己的阅读器,因为如果您在 64 位系统上运行,则无法获得正确的驱动程序。

如果您的软件将在 64 位系统上运行,请确保首先对其进行测试,并且 oledb 或 odbc 驱动程序将存在。

I know this doesn't answer your questions, but here's a word of warning.

I've had to create my own reader as you don't get the correct drivers if you ever run on a 64 bit system.

If your software will ever run on a 64 bit system, make sure you test it first and that the oledb or odbc drivers will be present.

魄砕の薆 2024-07-16 15:30:59

如果您需要快速顺序访问 CSV 文件,快速 CSV 阅读器 可能是一个选项。 我前段时间在一个项目中使用过它,取得了巨大的成功。 它应该经过很好的优化,并且如果需要的话还提供缓存版本。 此外,自 2005 年首次发布(最后一次更新于 2008 年 10 月 09 日)以来,它已更新多次,并且通过实现 System.Data.IDataReader 支持基本数据绑定。

In case that you need a fast sequential access to the CSV file, the Fast CSV Reader could be an option. I have used it on a project some time ago with great success. It is supposed to be optimized quite well and also provides a cached version, if you need it. Additionally, it was updated several times since it was first released back in 2005 (last update in 2008-10-09) and it supports basic databinding by implementing System.Data.IDataReader.

时光是把杀猪刀 2024-07-16 15:30:59

如果不推出自定义解决方案,我不确定是否有一种简单的方法来支持多个分隔符。 此页面建议通过 schema.ini 您可以选择:

  • TabDelimited
  • CSVDelimited
  • 一个特定字符(双引号除外)
  • 固定宽度

Without rolling a custom solution, I'm not sure there's a straightforward way to support more than one delimiter. This page suggests that through schema.ini you can choose between:

  • TabDelimited
  • CSVDelimited
  • one specific character (except double quote)
  • fixed width
要走就滚别墨迹 2024-07-16 15:30:59
    class CSVFile extends SplFileObject
{

private $keys;

    public function __construct($file)
    {
        parent::__construct($file);
        $this->setFlags(SplFileObject::READ_CSV);
    }

    public function rewind()
    {
        parent::rewind();
        $this->keys = parent::current();
        parent::next();
    }

    public function current()
    {
        return array_combine($this->keys, parent::current());
    }

    public function getKeys()
    {
        return $this->keys;
    }
}

然后使用 with:

$csv = new CSVFile('exmaple.csv');

并且您可以使用以下方式迭代行:

foreach ($csv as $line)
{
    class CSVFile extends SplFileObject
{

private $keys;

    public function __construct($file)
    {
        parent::__construct($file);
        $this->setFlags(SplFileObject::READ_CSV);
    }

    public function rewind()
    {
        parent::rewind();
        $this->keys = parent::current();
        parent::next();
    }

    public function current()
    {
        return array_combine($this->keys, parent::current());
    }

    public function getKeys()
    {
        return $this->keys;
    }
}

then use with:

$csv = new CSVFile('exmaple.csv');

and you can iterate through lines using:

foreach ($csv as $line)
{
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文