从文件中解析列名 [英] Parse column names from file

查看:73
本文介绍了从文件中解析列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从包含管道作为分隔符的文本文件中解析单词的问题



我的文件的值类似于

Problem to parse words from a text file that contains pipe as delimiter

I have a file that have values like

============================================================
Id | Name | Age |D | Type |P       |
   |      |     |O |      |H       |
   |      |     |B |      |Number  |
============================================================



我必须解析这些行并提取列名,即Id,Name,Age,DOB,键入,PHNumber成矢量。



我无法获得DOB和PHNumber名称。

请建议我应该如何解析这些行。



我的尝试:




I have to parse these lines and extract the column names which are Id,Name,Age,DOB,Type,PHNumber into a vector.

I am not able to get DOB and PHNumber names.
Please suggest how should I parse these lines.

What I have tried:

boost::regex col_check("^[A-Z].*");
while(getline(ifile,record))
    {
	   if(regex_match(record,col_check) )
		{
		    string tmp=record;
		    istringstream line(tmp);
		    while(getline(line,token,'|'))
		    {
		      rows.push_back(token);
		      
		      
		    }
	}
}

推荐答案

解析字符串是共同的道路。根据你在项目中的字符串类,你可以使用一些split或strtok函数。



我在这个有分割的文章 strtok兑换。找到子字符串后,您可能需要删除空格并将字符串转换为其他数据类型。
Parsing strings is a common path. Depending on what string class you have in your project you may use some split or strtok function.

A nice solution i found in this article with split and a article for strtok redeemed. After finding the substring you may need to remove white spaces and convert the string into some other data type.


这篇关于从文件中解析列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆