CSV(逗号分隔值文件格式)

逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。通常,所有记录都有完全相同的字段序列。

CSV文件格式的通用标准并不存在,但是在RFC 4180中有基础性的描述。使用的字符编码同样没有被指定,但是7-bitASCII是最基本的通用编码。

这种文件格式经常用来作为不同程序之间的数据交互的格式。

具体文件格式:每条记录占一行 以逗号为分隔符 逗号前后的空格会被忽略 字段中包含有逗号,该字段必须用双引号括起来 字段中包含有换行符,该字段必须用双引号括起来 字段前后包含有空格,该字段必须用双引号括起来 字段中的双引号用两个双引号表示 字段中如果有双引号,该字段必须用双引号括起来 第一条记录,可以是字段名

John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123

#include <iostream>
#include <fstream> ifstream file(m_strFilePath);
std::string row;
vector<string> infRow;
getline(file, row);//读取第一行
MySplit(row, infRow); //获取每列的内容 while (file.good())
{
//读取每一行
getline(file, row);
CStringA strRow = row.c_str();
strRow.Replace("\\", "\\\\");
strRow.Replace("'", "\\'");
row = strRow;
infRow.clear();
MySplit(row, infRow);
//...
} char *m_pcTemp;
int m_iMaxTempLength; m_pcTemp(NULL), m_iMaxTempLength() //csv
void PushInTemp(const char *pcCursor, int iLen)
{
if (iLen >= m_iMaxTempLength)
{
m_iMaxTempLength = iLen * ;
if (m_pcTemp)
{
delete[] m_pcTemp;
m_pcTemp = nullptr;
} m_pcTemp = new char[m_iMaxTempLength];
}
if (iLen > )
{
if (!m_pcTemp)
{
m_pcTemp = new char[m_iMaxTempLength];
} memcpy(m_pcTemp, pcCursor, iLen);
} if(m_pcTemp)
m_pcTemp[iLen] = '\0';
} //csv
void MySplit(std::string &row, vector<string> &infRow)
{
const char *pcCursor = row.c_str();
const char *pcComma = NULL;
const char *pcQuot = NULL; do
{
if ( == row.size())
{
break;
}
pcComma = strchr(pcCursor, ',');
pcQuot = strchr(pcCursor, '"'); if (NULL == pcComma && NULL == pcQuot)
{
infRow.push_back(pcCursor);
break;
}
if (NULL == pcQuot)
{
int iLen = (int)pcComma - (int)pcCursor; PushInTemp(pcCursor, iLen);
infRow.push_back(m_pcTemp);
pcCursor += iLen + ;
}
else if (NULL == pcComma)
{
const char *pcLastQuot = strrchr(pcCursor, '"');
int iLen = (int)pcLastQuot - (int)pcQuot - ; PushInTemp(pcQuot + , iLen);
infRow.push_back(m_pcTemp);
break;
}
else
{
int iCommaPos = (int)pcComma;
int iQuotPos = (int)pcQuot; if (iCommaPos < iQuotPos)
{
int iLen = (int)pcComma - (int)pcCursor;
PushInTemp(pcCursor, iLen);
infRow.push_back(m_pcTemp);
pcCursor += iLen + ;
}
else
{
const char *pcNextQuot = NULL;
int iMove = (int)pcQuot - (int)pcCursor + ;
std::string strQuotData; pcCursor += iMove; do
{
pcNextQuot = strchr(pcCursor, '"');
if (NULL == pcNextQuot)
{
goto end;
}
if (*(pcNextQuot + ) == '"')
{
int iLen = (int)pcNextQuot - (int)pcCursor;
PushInTemp(pcCursor, iLen);
strQuotData += m_pcTemp;
pcCursor = pcNextQuot + ;
strQuotData += '"';
}
else
{
int iLen = (int)pcNextQuot - (int)pcCursor;
PushInTemp(pcCursor, iLen);
strQuotData += m_pcTemp;
infRow.push_back(strQuotData);
pcCursor += iLen + ;
if (*pcCursor == ',')
{
++pcCursor;
}
break;
}
} while ();
}
}
if (*pcCursor == '\0')
{
break;
}
} while (); end:
return;
}

【CSV文件】CSV文件内容读取的更多相关文章

  1. 将文件中的内容读取到map中,并排除不需要的关键字然后输出

  2. python 修改文件中的内容

    在python的文件操作中,是没有办法对文件中具体某行或者某个位置的内容进行局部的修改的,如果需要对文件的某一行内容进行修改,可以先将文件中的所有的内容全部读取出来,再进行内容判断,是否是需要修改的内 ...

  3. 内容写到 csv 格式的文件中 及 读取 csv 格式的文件内容

    <?php/*把内容写到 csv 格式的文件中 基本思路是:1.用 $fp = fopen("filename", 'mode')打开一个csv文件,可以是打开时才建立的2. ...

  4. C# 读取CSV和EXCEL文件示例

    我们习惯了直接连到数据库上面读取数据表的数据内容: 如果有一天我们需要读取CSV,EXCEL文件的内容的时候,可不可以也像读数据表的方式一样呢?当然可以,使用OleDB ADO.NET是很简单的事情 ...

  5. R—读取数据(导入csv,txt,excel文件)

    导入CSV.TXT文件 read.table函数:read.table函数以数据框的格式读入数据,所以适合读取混合模式的数据,但是要求每列的数据数据类型相同. read.table读取数据非常方便,通 ...

  6. python读取txt、csv和excel文件

    一.python读取txt文件:(思路:先打开文件,读取文件,最后用for循环输出内容) fp = open('test.txt','r') lines = fp.readlines() fp.clo ...

  7. csv、json 文件读取

    1.CSV 文件存储 1.1 写入 简单示例 import csv with open('data.csv', 'a') as csvfile: writer = csv.writer(csvfile ...

  8. python读取与写入csv,txt格式文件

    python读取与写入csv,txt格式文件 在数据分析中经常需要从csv格式的文件中存取数据以及将数据写书到csv文件中.将csv文件中的数据直接读取为dict类型和DataFrame是非常方便也很 ...

  9. 计算机程序的思维逻辑 (64) - 常见文件类型处理: 属性文件/CSV/EXCEL/HTML/压缩文件

    对于处理文件,我们介绍了流的方式,57节介绍了字节流,58节介绍了字符流,同时,也介绍了比较底层的操作文件的方式,60节介绍了随机读写文件,61节介绍了内存映射文件,我们也介绍了对象的序列化/反序列化 ...

  10. python读写word、excel、csv、json文件

    http://blog.csdn.net/pipisorry/article/details/50368044 python读写word文档 (include wps)将word文档转换成txt文档 ...

随机推荐

  1. 动态LINQ(Lambda表达式)

    1.准备数据实体 public class Data { public string AccountNO { get; set; } public int Count { get; set; } } ...

  2. swfupload上传图片

    项目结构 以及插件需要的文件如图所示 前端代码: <!DOCTYPE html> <html> <head> <title>SWFUpload</ ...

  3. spring controller方法和jstl

    1复杂类型查询:查询条件已经多于一个实体类中的属性 1)可以创建一个类用于组合查询条件 基础类 public class Items { private Integer id; private Str ...

  4. Django安装与创建项目

    下载 https://media.djangoproject.com/releases/1.11/Django-1.11.20.tar.gz 解压 tar -zvxf Django-1.11.20.t ...

  5. Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile

    完整的错误信息: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile ( ...

  6. Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 解决方法: 要对切割字符进行转义\\

    使用str.split("[",15)时,出现Exception in thread "main" java.util.regex.PatternSyntaxE ...

  7. lua --- 点号 和 冒号

    冒号的作用:1.定义函数时,给函数添加隐藏的第一个参数 self2.调用函数时,默认把当前调用者作为第一个参数传递进去 如 a:b(c) 可以理解为 a.b(a, c) 以下是用点号的定义和调用函数的 ...

  8. Java Spring JDBC访问数据库

    一.首先采用org.springframework.jdbc.datasource.DriverManagerDataSource类进行实现 1.applicationContext.xml配置如下: ...

  9. fiddler学习笔记&&基本使用

    周末在网上找了些fiddler相关的资料来看,学习下如何使用这个工具(平时接口测试用得比较多,在没有接口文档的情况下,可以通过抓包工具来提取需要测试的接口,ps.好久没写博客了,争取5月结束前再写2篇 ...

  10. HeadFirst Ruby 第七章总结 references

    前言 这一章的内容关于 references,讲了当 Ruby 程序中可能会遇到关于 reference 与 object 之间概念混淆而导致的问题. 导言 本章从一个 astronomer 发现 s ...