Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add append support for CSVWriter #35

Open
jsm174 opened this issue Jul 26, 2017 · 2 comments
Open

Add append support for CSVWriter #35

jsm174 opened this issue Jul 26, 2017 · 2 comments

Comments

@jsm174
Copy link

jsm174 commented Jul 26, 2017

Hello. I was looking to have a CSV file that gets updated every time a user performs an action in an app. I also wanted the CSV file to contain a header row.

Subsequent appends won't be started on a new line because isFirstRow is defaulted to true, causing write to ignore the configuration.newline.unicodeScalars.forEach(writeScalar) call.

I thought I could just add a beginNewRow() call, but it technically doesn't modify the steam

Here is the code I was trying:

           let exists = FileManager.default.fileExists(atPath: url.path)
           
           let stream = OutputStream(url: url, append: true)!
           let csv = try! CSVWriter(stream: stream)
           
           if !exists {
               try! csv.write(row: ["Date", "Question 1", "Question 2", "Question 3"])
           }

           try! csv.write(row: [rfcDate, answer1, answer2, answer3])

           csv.stream.close()
@jsm174
Copy link
Author

jsm174 commented Jul 26, 2017

Until (or if ever) there is a fix, I've worked around by adding the new line directly to stream:

            let exists = FileManager.default.fileExists(atPath: url.path)
            
            let stream = OutputStream(url: url, append: true)!
            let csv = try! CSVWriter(stream: stream)
            
            if !exists {
                try! csv.write(row: ["Date", "Question 1", "Question 2", "Question 3"])
            }
            
            let rfcDate = Date().rfcString()
                        
            try! csv.write(row: [rfcDate, question1Answer, question2Answer, question3Answer])
            
            let data = [UInt8](csv.configuration.newline.utf8)
            csv.stream.write(data, maxLength: data.count)
            
            csv.stream.close()

@wizofaus
Copy link

Is it normal in a MacOS environment for CSV files NOT to be newline terminated?
I'm used to expecting CSV files to end with a newline (or CRLF), so if I created an OutputStream on such a file, this library would correctly append new lines - but leave me with a stream that no longer ends with a newline and hence require, as you do above, manually adding one before appending further lines.
If there really is a need to produce CSV files that aren't newline terminated, I suppose it could be an option (but I'd expect it to do by default).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants