在CSV中使用StrTokens/重新排列/重复值 [英] Using StrTokens/rearrange/repeat values in CSV

查看:63
本文介绍了在CSV中使用StrTokens/重新排列/重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已解决:
非常感谢 Richard MacCutchan 和其他对该线程做出了贡献的人!

因此,问题是我想从CSV文件中获取值,该文件的格式如下:

Fname1,Minitial1,LName1,Com1,(Desc1)
Fname2,Minitial2,LName2,Com2,(Desc2)
Fname3,Minitial3,LName3,Com3,(Desc3)

我要完成的工作是将每个用逗号分隔的令牌都分配给一个临时值.之所以要这样做,是因为我想将输入值重新排列为特定顺序,并能够多次输出相同的令牌.这是所需输出的示例:

LName1 Com1 Fname1最小1(Desc1)Fname1.LName1
LName2 Com2 Fname2 Minitial2(Desc2)Fname2.LName2
LName3 Com3 Fname3 Minitial3(Desc3)Fname3.LName3

如您所见,需要临时令牌[0]-4]才能完成此任务.为了我的特定目的,令牌需要重新排列为:

RESOLVED:
Big thanks to Richard MacCutchan and everyone else who contributed to the thread!

So, the problem was that I wanted to take values from a CSV file that is formatted something like this:

Fname1,Minitial1,LName1,Com1,(Desc1)
Fname2,Minitial2,LName2,Com2,(Desc2)
Fname3,Minitial3,LName3,Com3,(Desc3)

What I wanted to accomplish was take each token delimited by a comma and assign it to a temporary value. The reason why I wanted to do this was because I wanted to rearrange the input values in to a specific order as well as be able to output the same token multiple times. This is an example of the desired output:

LName1 Com1 Fname1 Minitial1 (Desc1) Fname1.LName1
LName2 Com2 Fname2 Minitial2 (Desc2) Fname2.LName2
LName3 Com3 Fname3 Minitial3 (Desc3) Fname3.LName3

As you can see, temporary tokens [0] - 4] are needed to be able to accomplish this. For my specific purpose, the tokens needed to be rearranged as:

outfile <<  szToken[2] << " ";
outfile <<  szToken[3] << " ";
outfile <<  szToken[0] << " ";
outfile <<  szToken[1] << " ";
outfile <<  szToken[4] << " ";
outfile <<  szToken[0] << ".";
outfile <<  szToken[2] << endl;




这是完整的代码:




Here is the entire code:

// reading a text file
#include iostream
#include fstream
#include string

using namespace std;

int main () {
  string line;
  ifstream infile ("infile.csv");
  ofstream outfile ("outfile.txt");
if (infile.is_open())
    {
        while (getline(infile, line))
        {
            // copy the string to a character array
            char* pszLine = new char[line.length() + 1];
            strcpy(pszLine, line.c_str());
            char* szToken[5];
 
            // split the line into tokens
            szToken[0] = strtok(pszLine, ",");
            for (int i = 1; i < 5; ++i)
            {
                szToken[i] = strtok(NULL, ",");
            }
 
            // write the tokens out in a different order
            outfile <<  szToken[2] << " ";
            outfile <<  szToken[3] << " ";
            outfile <<  szToken[0] << " ";
            outfile <<  szToken[1] << " ";
            outfile <<  szToken[4] << " ";
            outfile <<  szToken[0] << ".";
            outfile <<  szToken[2] << endl;
 
            // tidy up
            delete []pszLine;
        }
}

}



实际上,这最终将被用于生成VB脚本.


再次感谢大家,希望下次我在这里发布时,它可以帮助像你们一样的人帮助我!



This will actually end up being used to produce VB scripts.


Thanks again to everyone, and I hope the next time I post here, it''s helping someone like you guys helped me!

推荐答案

您可能需要拆分您的串成令牌.读完后,可以将字符串数据复制到char数组,并使用 tokenizer之一 [ ^ ]函数可以将其分解为组成子字符串.

[/edit]添加了代码段.[/edit]
You probably need to split your string into tokens. Once you have read it in you can copy the string data to a char array and use one of the tokenizer[^] functions to break it up into its constituent sub-strings.

[/edit]Added code snippet.[/edit]
PTSTR szToken[5];
szToken[0] = strtok(szText, szSeparators);
for (int i = 1; i < 5; ++i)
{
    szToken[i] = strtok(NULL, szSeparators);
}


szToken[0]到5现在包含您的令牌


szToken[0] to 5 now contain your tokens


确定,建立在您的代码上:
OK, building on your code:
if (myfile.is_open())
{
    while (getline(myfile, line))
    {
        // copy the string to a character array
        char* pszLine = new char[line.length() + 1];
        strcpy(pszLine, line.c_str());
        char* szToken[5];

        // split the line into tokens
        szToken[0] = strtok(pszLine, ",");
        for (int i = 1; i < 5; ++i)
        {
            szToken[i] = strtok(NULL, ",");
        }

        // write the tokens out in a different order
        cout << szToken[0] << ".";
        cout << szToken[3] << ".";
        cout << szToken[4] << ".";
        cout << szToken[2] << ".";
        cout << szToken[1] << endl;

        // tidy up
        delete []pszLine;
    }
}


我将让您将输出字符串写到文件而不是cout.


I''ll leave you to write the output strings to a file rather than cout.


如果您可以写得更清楚,那么事情会容易得多. 此处是一个很好的示例:"C ++解析拆分分隔字符串您可以适应自己的需求.
If you could write you question more clear, things could be much easier. Here is a good sample "C++ Parse Split Delimited String" that you can adapt to your needs.


这篇关于在CSV中使用StrTokens/重新排列/重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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