0

I would like the community's thoughts on a problem. I created a save and load state in my program. When I save, I create a *.csv file which saves out the information in a certain format. When I load, it loads the parsed *.csv file. The issue is when I parse, on my second line it can have as many lines as the user wants because it's a description text box. The format of the csv is like this:

User title - 1 line

User description - any number of lines

Header -1 line

Address locations -1 line per address

Example:

Myawesometitle,

This is a description,

A very good description,

Nbr,address, city,state,zip

1,5643 marketstreet, Cincinnati, OH, 80985

2,12345 main avenue, Denver, CO, 67890

How can I get around the user description having multiple lines? I can parse by either /n or ,

Thank you,

4
  • 2
    Use a ready-made CSV reader/writer that will deal with newlines for you, or better, choose a better, structured, format than CSV, like JSON or XML. Commented May 22, 2016 at 13:18
  • why not: parse the first line as user title, then read line until you reach the unique Header line (maybe put some ** symbols in that line that will allow you to identify it) then continue parsing lines. Commented May 22, 2016 at 13:20
  • 1
    CSV files are composed of lines and fields. Lines are separated by \n character, fields by , or ; character. If a field contains \n , or ; this creates problems parsing the file. The convention is, in this case, to write the string containing problematic characters inside double quotes ". The parser must check for double quotes and exclude interpreting all \n , and ; that are written inside double quotes Commented May 22, 2016 at 14:04
  • it does have to be CSV =*( but thanks for your comments! user3494047 I believe that would work thanks again! Commented May 22, 2016 at 17:43

1 Answer 1

2

If you are not obligated to use CSV specifically I encourage you to use JSON format, it is much more dynamic and schema free, because for example, you can use lists [] as a value, and in you case it suites perfectly to unlimited number of addresses.

For example:

{
     "User title" : "Myawesometitle"

     "User description" : "This is a description"

     "Header" : "A very good description"

     "Address locations" :
     [
          {
               "Nbr" : "1,5643"
               "address" : "marketstreet"
               "city" : "Cincinnati"
               "state" : "OH"
               "zip" : "80985"
          }, 
          {
               "Nbr" : "2,12345"
               "address" : "main avenue"
               "city" : "Denver"
               "state" : "CO"
               "zip" : "67890"
          }
    ]
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.