Qt语言如何读取CSVQt 大文件读取

TensorFlow - 读取CSV格式文件
TensorFlow - 读取CSV格式文件
以TensorFlow提供的例子说明
filename_queue = tf.train.string_input_producer([&file0.csv&, &file1.csv&])
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [[1], [1], [1], [1], [1]]
col1, col2, col3, col4, col5 = tf.decode_csv(
value, record_defaults=record_defaults)
features = tf.stack([col1, col2, col3, col4])
with tf.Session() as sess:
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(1200):
# Retrieve a single instance:
example, label = sess.run([features, col5])
coord.request_stop()
coord.join(threads)
API说明concat
import tensorflow as tf
t1 = [[1, 2, 3],[4, 5, 6]]
t2 = [[7, 8, 9],[10, 11, 12]]
result =(tf.concat( [t1,t2],0))
sess = tf.Session()
print(sess.run(result))
tf.concat( [t1,t2],0)的结果是
[10 11 12]]
tf.concat( [t1,t2],1)的结果是
[[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
API说明stack
import tensorflow as tf
result =tf.stack([x, y, z],0)
sess = tf.Session()
print(sess.run(result))
tf.stack([x, y, z],0)的结果是
tf.stack([x, y, z],1)的结果是
To read text files in comma-separated value (CSV) format, use a
tf.TextLineReader with the tf.decode_csv operation.
读取CSV格式的文本文件, 需要使用TextLineReader和decode_csv 操作
Each execution of read reads a single line from the file. The
decode_csv op then parses the result into a list of tensors. The
record_defaults argument determines the type of the resulting &tensors and sets thedefault value to use if a value is missing in the &input string.
每次执行read都会从文件中读取一行内容, decode_csv 操作会将内容解析到张量列表。record_default参数决定了结果的张量的类型,并设置了如果在输入字符串中缺失值时的默认值。
You must call tf.train.start_queue_runners to populate the queue
before you call run or eval to execute the read. Otherwise read will
block while it waits for filenames from the queue.
你必须在调用run或者eval去执行read之前, 调用tf.train.start_queue_runners来填充队列。否则read操作会阻塞。java 读取csv文件里指定行列的值,比如读取第三行第二列的值。_百度知道
java 读取csv文件里指定行列的值,比如读取第三行第二列的值。
void test(int row,int col){
BufferedReader reader = new BufferedReader(new FileReader(&C:\\a.csv&));//换成你的文件名
reader.readLine();//第一行信息,为标题信息,不用,如果需要,注释掉
我有更好的答案
  java读取csv文件,按照指定格式:  import java.io.IOE  import java.nio.charset.C  import java.util.ArrayL  import com.csvreader.CsvR  import com.csvreader.CsvW  /**  * 读取CSV文件  * 所谓&CSV&,是Comma Separated Value(逗号分隔值)的英文缩写,通常都是纯文本文件。  * 可以看成数据库程序与电子表格之间一种中间通信文件,数据库可以导出。csv格式,excel也可以导入并打开。csv文件,例子如下  * sj_mino1001.jpg,FB55FE8,  * sj_mino1002.jpg,03C5C,  * sj_mino1003.jpg,E80467,  *  */  public class CSVDeal{  public static void main(String[] args) {  try {  String[] stringL  String csvFilePath = &C:\\Users\\Administrator\\Desktop\\36.csv&;  String sourceFileString= &C:\\Users\\Administrator\\Desktop\\test.csv&;  CsvReader reader = new CsvReader(csvFilePath); //默认是逗号分隔符,UTF-8编码  CsvWriter writer = new CsvWriter(sourceFileString);  /*  * readRecord()判断是否还有记录,getValues()读取当前记录,然后指针下移  */  reader.readRecord();  writer.writeRecord(reader.getValues()); //读取表头  /*  * 逐行读取,以免文件太大  * 处理表头后面的数据,这里是在第12列数据统一加前缀&V&  */  while(reader.readRecord()){  stringList = reader.getValues();  stringList[11] = 'V' + stringList[11];  writer.writeRecord(stringList);  }  reader.close();  writer.close();  }catch(Exception ex){  System.out.println(ex);  }  }  }
import java.io.BufferedRimport java.io.FileRpublic class Test {
void test(int row,int col){
BufferedReader reader = new BufferedReader(new FileReader("C:\\a.csv"));//换成你的文件名
// reader.readLine();//第一行信息,为标题信息,不用,如果需要,注释掉
String line =
int index=0;
while((line=reader.readLine())!=null){
String item[] = line.split(" ");//CSV格式文件为逗号分隔符文件,这里根据逗号切分
if(index==row-1){
if(item.length&=col-1){
String last = item[col-1];//这就是你要的数据了
System.out.println(last);
//int value = Integer.parseInt(last);//如果是数值,可以转化为数值
} catch (Exception e) {
e.printStackTrace();
* @param args
public static void main(String[] args) {
Test test = new Test();
test.test(3, 2);
}}你的数据格式有问题,空格的个数不确定,没办法每行用空格分隔。以下是我调整后的数据格式每行的数据以一个空格分隔,test方法传入的参数一次是,行,列:1&电机&12&WBS&23&PID&34&CP&5&社供出6&原価実绩&7&社供WC&8&外注费&9&直材费&10&自家制品&11&直経费&12&その他&13&注残&14&注残
虽然程序可运行,但是查询的结果不对,自己随意换了坐标位置,查询就报异常或者结果出错了,汗……
首先把你CSV文件的内容改成上面的,每行的分隔符为一个空格。把15行的col-1改成col,如果列数超过范围则不输出。输入的参数依次为,行数,列数。比如输入test(3,2),显示第三行第二列,PID
本回答被提问者和网友采纳
为您推荐:
其他类似问题
csv的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。QT读取指定行文件的问题
[问题点数:40分]
本版专家分:5
结帖率 38.46%
CSDN今日推荐
本版专家分:200
本版专家分:200
匿名用户不能发表回复!|
其他相关推荐C++读取CSV表格
看到这个题目可能会有人问什么是CSV表格呢,在我做这个实现之前我也是没有听说过CSV表格,更不知道他有什么用了。CSV是一种Excel表格的导出格式,在Excel表格的菜单栏中点击文件-&另存为会弹出一个文件夹浏览窗口,在下拉框中可以选择保存格式,其中有一个就是.CSV(逗号分隔符)选项。
CSV表格的规则:
1 开头是不留空,以行为单位。
2 可含或不含列名,含列名则居文件第一行。
3 一行数据不垮行,无空行。
4 以半角逗号(即,)作分隔符,列为空也要表达其存在。
5 列内容如存在半角逗号(即,)则用半角引号(即&&)将该字段值包含起来。
6 列内容如存在半角引号(即&)则应替换成半角双引号(&&)转义,并用半角引号(即&&)将该字段值包含起来。
7 文件读写时引号,逗号操作规则互逆。
8 内码格式不限,可为 ASCII、Unicode 或者其他。
9 不支持特殊字符
CSV表格的用途:
CSV表格也相当于关系中的二维表,但是他并不支持SQL语句的查询,只能通过一些手段将表格内容存入合适的数据结构中便于查询。如果结构选择的合适,那么查询起来还是很方便的,但是他不利于表格中数据的更新,更新就好比是文件的操作一样,各种文件指针游来游去,很繁琐。如果程序中要使用外部数据,数据又不多不值当去用数据库的话,就可以考虑使用CSV表格存储外部数据,每当程序执行时可以从外部读取数据进入内存,避免了程序因数据的改动而重新编译。
使用C++读取CSV表格的大致思路:
1:以打开文件的方式打开CSV表格,通过文件指针的各种游走,划分出每一行,将一行数据存到一个string对象中。循环读取文件中的每一行,文件读取完毕后悔生成一个map&int, string&这样的对象,int表示行号,string表示这一行中的所有字符。使用到的函数是strchr
2:再对每一行进行分割,分割的标准就是逗号。这样对每一行来说会被分割出cols个string对象,把这些对象也存储在一个map&int, string&对象中,这里的int表示从左到右的列号,从1数起。这样每一行数据会生成一个
map&int, string&对象,处理完一个文本会生成一个map&int, map&int, stirng&&对象。利用一个二重的映射关系将表格中的每一个字段全部有规律的存储起来。
下面是具体的实现代码:
#pragma once&&
//#include &stringparser.h&&&
#include &assert.h&&&
#include &map&&&
#include &vector&&&
#include &string&&&
#include &SkillRecord.h&&&
typedef unsigned long&& u32;&
class CppCSV&
&&& map&u32, map&u32, string&& m_stringM&
&&& string m_CSVN&
&&& CppCSV(){}&
&&& CppCSV(const char *path)&
&&&&&&& assert(LoadCSV(path));&
&&& ~CppCSV(){}&
&&& bool LoadCSV(const char *path);&
&&& bool SaveCSV(const char *path = NULL);&
&&& bool GetIntValue(u32 uiRow, u32 uiCol, int &riValue);&
&&& bool GetFloatValue(u32 uiRow, u32 uiCol, float &rfValue);&
&&& string* GetStringValue(u32 uiRow, u32 uiCol);&
&&& int GetParamFromString(string str, vector&string& &stringVec, char delim& = ',');&
&&& map&u32, map&u32, string&&& GetCSVMap()&
&&&&&&& return m_stringM&
&&& void GetSkillRecordMapTable(map&int, SkillRecord& &sSkillMapTable);&
#pragma once
//#include &stringparser.h&
#include &assert.h&
#include &map&
#include &vector&
#include &string&
#include &SkillRecord.h&
typedef unsigned long&& u32;
class CppCSV
&map&u32, map&u32, string&& m_stringM
&string m_CSVN
&CppCSV(){}
&CppCSV(const char *path)
&&assert(LoadCSV(path));
&~CppCSV(){}
&bool LoadCSV(const char *path);
&bool SaveCSV(const char *path = NULL);
&bool GetIntValue(u32 uiRow, u32 uiCol, int &riValue);
&bool GetFloatValue(u32 uiRow, u32 uiCol, float &rfValue);
&string* GetStringValue(u32 uiRow, u32 uiCol);
&int GetParamFromString(string str, vector&string& &stringVec, char delim& = ',');
&map&u32, map&u32, string&&& GetCSVMap()
&&return m_stringM
&void GetSkillRecordMapTable(map&int, SkillRecord& &sSkillMapTable);
#include &CppCSV.h&&&
#include &stdio.h&&&
//#include &stringparser.h&&&
bool CppCSV::LoadCSV(const char *path)&
&&& FILE *pFile = fopen(path, &r&);&
&&& if (pFile)&
&&&&&&& fseek(pFile, 0, SEEK_END);&
&&&&&&& u32 uSize = ftell(pFile);&
&&&&&&& rewind(pFile);&
&&&&&&& char *fileBuffer = new char[uSize];&
&&&&&&& fread(fileBuffer, 1, uSize, pFile);&
&&&&&&& map&u32, string& stringM&
&&&&&&& u32 uiIndex = 1;&
&&&&&&& char *pBegin = fileB&
&&&&&&& char *pEnd = strchr(pBegin, '\n');&
&&&&&&& pBegin = pEnd + 1;&
&&&&&&& pEnd = strchr(pBegin, '\n');&
&&&&&&& while (pEnd)&
&&&&&&& {&
&&&&&&&&&&& string strT&
&&&&&&&&&&& strTemp.insert(0, pBegin, pEnd-pBegin);&
&&&&&&&&&&& assert(!strTemp.empty());&
&&&&&&&&&&& stringMap[uiIndex++] = strT&
&&&&&&&&&&& pBegin = pEnd + 1;&
&&&&&&&&&&& pEnd = strchr(pBegin, '\n');&
&&&&&&& }&
&&&&&&& delete []fileB&
&&&&&&& fileBuffer = NULL;&
&&&&&&& pBegin = NULL;&
&&&&&&& pEnd = NULL;&
&&&&&&& map&u32, string&::iterator iter = stringMap.begin();&
&&&&&&& for (; iter != stringMap.end(); ++iter)&
&&&&&&& {&
&&&&&&&&&&& vector&string& stringV&
&&&&&&&&&&& map&u32, string& stringMapT&
&&&&&&&&&&& assert(GetParamFromString(iter-&second, stringVec) & 0);&
&&&&&&&&&&&&&
&&&&&&&&&&& vector&string&::size_type idx = 0;&
&&&&&&&&&&& for (; idx != stringVec.size(); ++idx)&
&&&&&&&&&&& {&
&&&&&&&&&&&&&&& stringMapTemp[idx + 1] = stringVec[idx];&
&&&&&&&&&&& }&
&&&&&&&&&&& m_stringMap[iter-&first] = stringMapT&
&&&&&&& }&
&&&&&&& fclose(pFile);&
&&&&&&& m_CSVName =&
bool CppCSV::SaveCSV(const char *path /* = NULL */)&
&&& if (path != NULL)&
&&&&&&& m_CSVName =&
&&& FILE *pFile = fopen(m_CSVName.c_str(), &w&);&
&&& if (pFile)&
&&&&&&& map&u32, map&u32, string&&::iterator iter = m_stringMap.begin();&
&&&&&&& for (; iter != m_stringMap.end(); ++iter)&
&&&&&&& {&
&&&&&&&&&&& map&u32, string& &rStringMap = iter-&&
&&&&&&&&&&& map&u32, string&::iterator it = rStringMap.begin();&
&&&&&&&&&&& for (; it != rStringMap.end(); ++it)&
&&&&&&&&&&& {&
&&&&&&&&&&&&&&& string strTemp = it-&&
&&&&&&&&&&&&&&& strTemp += ',';&
&&&&&&&&&&&&&&& fwrite(strTemp.c_str(), 1, 1, pFile);&
&&&&&&&&&&& }&
&&&&&&&&&&& char delim = '\n';&
&&&&&&&&&&& fwrite(&delim, 1, 1, pFile);&
&&&&&&& }&
&&&&&&& fclose(pFile);&
bool CppCSV::GetIntValue(u32 uiRow, u32 uiCol, int &riValue)&
&&& string *pStr = GetStringValue(uiRow, uiCol);&
&&& if (pStr)&
&&&&&&& riValue = atoi(pStr-&c_str());&
bool CppCSV::GetFloatValue(u32 uiRow, u32 uiCol, float &rfValue)&
&&& string *pStr = GetStringValue(uiRow, uiCol);&
&&& if (pStr)&
&&&&&&& rfValue = atof(pStr-&c_str());&
string* CppCSV::GetStringValue(u32 uiRow, u32 uiCol)&
&&& map&u32, map&u32, string&&::iterator iter = m_stringMap.find(uiRow);&
&&& if (iter != m_stringMap.end())&
&&&&&&& map&u32, string& &rStrMap = iter-&&
&&&&&&& map&u32, string&::iterator it = rStrMap.find(uiCol);&
&&&&&&& if (it != rStrMap.end())&
&&&&&&& {&
&&&&&&&&&&& return &(it-&second);&
&&&&&&& }&&
&&&&&&& else&
&&&&&&& {&
&&&&&&&&&&& return NULL;&
&&&&&&& }&
&&&&&&& return NULL;&
//用于分割字符串,将CSV表格中的一行按照规则解析成一组字符串,存储在一个vector中&&
//根据CSV表格中所存储的数据的不同,重载各函数&&
int CppCSV::GetParamFromString(string str, vector&string& &stringVec, char delim)&
&&& char *token = strtok(const_cast&char *&(str.c_str()), &delim);&
&&& while (token)&
&&&&&&& string strTemp =&
&&&&&&& stringVec.push_back(strTemp);&
&&&&&&& token = strtok(NULL, &delim);&
&&& return stringVec.size();&
void CppCSV::GetSkillRecordMapTable(map&int, SkillRecord& &sSkillMapTable)&
&&& map&u32, map&u32, string&&::iterator iter = m_stringMap.begin();&
&&& for (; iter != m_stringMap.end(); ++iter)&
&&&&&&& map&u32, string& strmap = iter-&&
&&&&&&& SkillRecord skillT&
&&&&&&& skillTemp.SetID(atoi(strmap[1].c_str()));&
&&&&&&& skillTemp.SetPath(strmap[2]);&
&&&&&&& skillTemp.SetName(strmap[3]);&
&&&&&&& skillTemp.SetHurt(atoi(strmap[4].c_str()));&
&&&&&&& skillTemp.SetPlayTime(atoi(strmap[5].c_str()));&
&&&&&&& sSkillMapTable[skillTemp.GetID()] = skillT&
#include &CppCSV.h&
#include &stdio.h&
//#include &stringparser.h&
bool CppCSV::LoadCSV(const char *path)
&FILE *pFile = fopen(path, &r&);
&if (pFile)
&&fseek(pFile, 0, SEEK_END);
&&u32 uSize = ftell(pFile);
&&rewind(pFile);
&&char *fileBuffer = new char[uSize];
&&fread(fileBuffer, 1, uSize, pFile);
&&map&u32, string& stringM
&&u32 uiIndex = 1;
&&char *pBegin = fileB
&&char *pEnd = strchr(pBegin, '\n');
&&pBegin = pEnd + 1;
&&pEnd = strchr(pBegin, '\n');
&&while (pEnd)
&&&string strT
&&&strTemp.insert(0, pBegin, pEnd-pBegin);
&&&assert(!strTemp.empty());
&&&stringMap[uiIndex++] = strT
&&&pBegin = pEnd + 1;
&&&pEnd = strchr(pBegin, '\n');
&&delete []fileB
&&fileBuffer = NULL;
&&pBegin = NULL;
&&pEnd = NULL;
&&map&u32, string&::iterator iter = stringMap.begin();
&&for (; iter != stringMap.end(); ++iter)
&&&vector&string& stringV
&&&map&u32, string& stringMapT
&&&assert(GetParamFromString(iter-&second, stringVec) & 0);
&&&vector&string&::size_type idx = 0;
&&&for (; idx != stringVec.size(); ++idx)
&&&&stringMapTemp[idx + 1] = stringVec[idx];
&&&m_stringMap[iter-&first] = stringMapT
&&fclose(pFile);
&&m_CSVName =
bool CppCSV::SaveCSV(const char *path /* = NULL */)
&if (path != NULL)
&&m_CSVName =
&FILE *pFile = fopen(m_CSVName.c_str(), &w&);
&if (pFile)
&&map&u32, map&u32, string&&::iterator iter = m_stringMap.begin();
&&for (; iter != m_stringMap.end(); ++iter)
&&&map&u32, string& &rStringMap = iter-&
&&&map&u32, string&::iterator it = rStringMap.begin();
&&&for (; it != rStringMap.end(); ++it)
&&&&string strTemp = it-&
&&&&strTemp += ',';
&&&&fwrite(strTemp.c_str(), 1, 1, pFile);
&&&char delim = '\n';
&&&fwrite(&delim, 1, 1, pFile);
&&fclose(pFile);
bool CppCSV::GetIntValue(u32 uiRow, u32 uiCol, int &riValue)
&string *pStr = GetStringValue(uiRow, uiCol);
&if (pStr)
&&riValue = atoi(pStr-&c_str());
bool CppCSV::GetFloatValue(u32 uiRow, u32 uiCol, float &rfValue)
&string *pStr = GetStringValue(uiRow, uiCol);
&if (pStr)
&&rfValue = atof(pStr-&c_str());
string* CppCSV::GetStringValue(u32 uiRow, u32 uiCol)
&map&u32, map&u32, string&&::iterator iter = m_stringMap.find(uiRow);
&if (iter != m_stringMap.end())
&&map&u32, string& &rStrMap = iter-&
&&map&u32, string&::iterator it = rStrMap.find(uiCol);
&&if (it != rStrMap.end())
&&&return &(it-&second);
&&&return NULL;
&&return NULL;
//用于分割字符串,将CSV表格中的一行按照规则解析成一组字符串,存储在一个vector中
//根据CSV表格中所存储的数据的不同,重载各函数
int CppCSV::GetParamFromString(string str, vector&string& &stringVec, char delim)
&char *token = strtok(const_cast&char *&(str.c_str()), &delim);
&while (token)
&&string strTemp =
&&stringVec.push_back(strTemp);
&&token = strtok(NULL, &delim);
&return stringVec.size();
void CppCSV::GetSkillRecordMapTable(map&int, SkillRecord& &sSkillMapTable)
&map&u32, map&u32, string&&::iterator iter = m_stringMap.begin();
&for (; iter != m_stringMap.end(); ++iter)
&&map&u32, string& strmap = iter-&
&&SkillRecord skillT
&&skillTemp.SetID(atoi(strmap[1].c_str()));
&&skillTemp.SetPath(strmap[2]);
&&skillTemp.SetName(strmap[3]);
&&skillTemp.SetHurt(atoi(strmap[4].c_str()));
&&skillTemp.SetPlayTime(atoi(strmap[5].c_str()));
&&sSkillMapTable[skillTemp.GetID()] = skillT
}测试代码:
CppCSV cs(&skill.csv&);&
map&u32, map&u32, string&& stringMap = cs.GetCSVMap();&
map&u32, map&u32, string&&::iterator iter = stringMap.begin();&
for (; iter != stringMap.end(); ++iter)&
&&& map&u32, string& strmap = iter-&&
&&& map&u32, string&::iterator it = strmap.begin();&
&&& for (; it != strmap.end(); ++it)&
&&&&&&& cout&&it-&second&&&& &;&
&&& cout&&&
&CppCSV cs(&skill.csv&);
&map&u32, map&u32, string&& stringMap = cs.GetCSVMap();
&map&u32, map&u32, string&&::iterator iter = stringMap.begin();
&for (; iter != stringMap.end(); ++iter)
&&map&u32, string& strmap = iter-&
&&map&u32, string&::iterator it = strmap.begin();
&&for (; it != strmap.end(); ++it)
&&&cout&&it-&second&&&& &;
由于从文件中读取出来的内容都是存储在string对象中,如果文件呢存储的是一定意义的数据,需要进行转换了。简简单单,平平淡淡
Qt 从csv文件中读取数据
最近项目中提出的需求是从excel表格中导入数据,查阅了很多资料之后,发现直接操作xls格式文件并不容易,之后找到了一个比较好的解决办法,那就是把xls文件另存为csv文件,然后在程序中进行操作。首先大致说明一下这两种格式的区别:
xls文件是Excel电子表格的文件格式,而csv是一种比较通用的文件格式,xls文件只能用Excel才能打开,而csv文件可以用Excel、记事本、文本编辑器打开。
好了,说明了它们的不同之后我们就说一下大体思路,读取csv文件中的数据,就是先以换行符进行断开,这样就能获取到每行的数据,然后再以半角的逗号断开,这样就能获取到具体每个单元格中的数据,话不多说,上代码:QFileDialog* fd = new QFileDialog(this);//创建打开文件对话框
QString fileName = fd-&getOpenFileName(this,tr("Open File"),"/home",tr("Excel(*.csv)"));
if(fileName == "")
QDir dir = QDir::current();
QFile file(dir.filePath(fileName));
if(!file.open(QIODevice::ReadOnly))
qDebug()&&"OPEN FILE FAILED";
QTextStream * out = new QTextStream(&file);//文本流
QStringList tempOption = out-&readAll().split("\n");//每行以\n区分
for(int i = 0 ; i & tempOption.count() ; i++)
QStringList tempbar = tempOption.at(i).split(",");//一行中的单元格以,区分
...//省略具体对数据的操作
file.close();//操作完成后记得关闭文件
没有更多推荐了,}

我要回帖

更多关于 语言读取未知大小的txt文件 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信