Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Regarding reading CSV file



  • Hi All

    I have written a code to read the *.csv file and is working fine.Below is my code
    @
    QFile file(filename);
    QTextStream in(&file);
    if (file.open(QIODevice::ReadOnly))
    {
    QStringList listdata;
    QString line = in.readLine();
    listdata = line.split(',');
    int iHeaderCount = listdata.count();
    *HeaderCnt = iHeaderCount;
    while (!line.isNull())
    {
    line = in.readLine();
    line.remove(" ");
    listdata.append(line.split(','));
    }
    }
    @

    But when my data in *.csv file is as below

    "1","2","3","4","11,22,33","4","5","6","7"

    I want to read 11,22,33 as one value.How to do that?


  • Moderators

    If you've got control over CSV file itself, the easiest way would be, of course, to use another delimiter (like ';'), then reading would be pretty straightforward.

    Here, if you want to check for those 11,22,33 sequences AFTER splitting, you can check the resulting strings for '"' character. A lack of at leas one would indicate, that you are inside the sequence. This does not seem to be a very elegant way, though.

    If you want to check BEFORE splitting, you might want to consider using '"' as the delimiter - this way you would get loads of rubbish results (','), but they can be easily filtered out.

    Or, as probably the easiest route, you can simply use "," (quote, comma, quote) as your delimiter.



  • See "this thread":http://developer.qt.nokia.com/forums/viewthread/6893 and "this thread":http://developer.qt.nokia.com/forums/viewthread/7704 for further reference and possible solutions.

    You should avoid writing your own CSV parser if it is by any means possible for you to use an existing solution!


  • Moderators

    [quote author="Volker" date="1316043097"]See "this thread":http://developer.qt.nokia.com/forums/viewthread/6893 and "this thread":http://developer.qt.nokia.com/forums/viewthread/7704 for further reference and possible solutions.

    You should avoid writing your own CSV parser if it is by any means possible for you to use an existing solution![/quote]

    Rajveer, judging from Volker's links, you've already asked about CSV parsing twice, and been given elaborate and instructive responses every time. Try being a bit more considerate about other peoples' time in the future, please.



  • Hi sierdzio

    I have asked the question twice but both the time it was not same.
    And now also i have some specific doubt about parsing csv not the repeated doubt.



  • [quote author="Rajveer" date="1316082148"]Hi sierdzio

    I have asked the question twice but both the time it was not same.
    And now also i have some specific doubt about parsing csv not the repeated doubt.
    [/quote]

    As you could guess yourself, the advice stays the same: take a ready-to-use solution and avoid coding one yourself.




Log in to reply