Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I get QIODevice::readLine to ignore newline embedded within quotes?
Forum Updated to NodeBB v4.3 + New Features

How can I get QIODevice::readLine to ignore newline embedded within quotes?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 2.0k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DidgetMaster
    wrote on last edited by
    #1

    I am trying to parse a CSV file that has fields with embedded newlines ("\n" enclosed within quotes). I want readLine() to ignore those newlines. Is there a way to do that?

    Example:
    "first field","second field","this is a field\nwith two lines","last field"\n

    Currently readLine() will only return the the first two and a half fields because it stops at the first newline. It would be great if there was a readLine(bool ignoreNewlinesWithinQuotes = false) method. Any ideas on how to make this work with existing code?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Unless your file is huge, you might have better luck reading the whole file and use QRegularExpression to parse it.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DidgetMaster
        wrote on last edited by
        #3

        That's my problem. The file is huge and I don't want to read the whole thing into memory. I just want to process it a line at a time.

        1 Reply Last reply
        0
        • Paul H.P Offline
          Paul H.P Offline
          Paul H.
          wrote on last edited by Paul H.
          #4

          How about counting the quotation marks in the just read line, and if the number is odd, read the next line and append it to the previous line, repeat until the count is even on the appended string.
          Paul

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jeroen3
            wrote on last edited by
            #5

            Subclass QFile and create readLine(bool ignoreNewlinesWithinQuotes = false) yourself?

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved