skelpo / csv Goto Github PK
View Code? Open in Web Editor NEWA simple CSV file parser and serializer
Home Page: http://www.skelpo.codes/CSV/
License: MIT License
A simple CSV file parser and serializer
Home Page: http://www.skelpo.codes/CSV/
License: MIT License
@calebkleveter I'm using CSV to load data into a database and I'm combining onHeader and onCell to effectively build a row dictionary. Do you think that is a common enough use case to merit including something like an onRow:
handler in Parser.swift
?
Characters are lost when parsing a slightly modified version of your test data:
let data = """ first name,last_name,age,gender,tagLine TinTin,,16,M,"=HYPERLINK(""https://en.wikipedia.org/wiki/The_Great_Snake"",""Great snakes!"")" """
CSV produces the following when processing the last field:
Source:
=HYPERLINK(""https://en.wikipedia.org/wiki/The_Great_Snake"",""Great snakes!"")
Parsed Result:
=HYPERLINK("https://en.wikipedia.org/wiki/The_Great_Snak""Great snakes"
its missing the 'e' in Snake, the ',' after Snake", the '!' and the '")" at the end:
I updated the cellDelimiter processing line 128 in Parser.swift with the following:
case true: index += 1; slice.end += 1
The test that failed with the above data now passes and with an update of the test data all your other tests pass as well.
When encoding, we have to pass an array to the encoder:
try encoder.encode([obj1, obj2])`
but when decoding we have to pass the type of the object in the array and not the type of the array itself:
try decoder.decode(MyObject.self, from: data)
Even if this is briefly mentioned in the readme, it's not intuitive and unexpected, since when dealing with the familiar coders (JSONDecoder
, PropertyListDecoder
) we have to pass the same type on encoding and decoding.
When encoding or serializing data to CSV format, we don't escape double-quote characters ("
), which will causes the data to be parsed incorrectly. We need to escape the quote characters in the cell contents by adding another double-quote before it:
"Exactly!" He replied
Becomes this:
""Exactly!"" He replied
A main use case I have for this package is ingesting CSV files in Swift from exported existing database tables. Many database tables have snake_cased column names or don't exactly match the property in Swift . It would be convenient to add a keyDecodingStrategy
option similar to that of Swifts JSONDecoder
so that the values can be mapped without having to rename the csv column headers.
Take this code:
struct Asd: Codable {
var asd: String
var zuio = 0
}
let config = Config(cellSeparator: UInt8(ascii: ","), cellDelimiter: nil)
let encoder = CSVEncoder(configuration: config).sync
let data = try! encoder.encode([Asd(asd: "asd,fgh"), Asd(asd: "bla")])
let string = String(decoding: data, as: UTF8.self)
print(string)
let decoder = CSVDecoder(configuration: config).sync
do {
let object = try decoder.decode(Asd.self, from: data)
print(object)
} catch {
print(error)
}
It fails with error:
"Cannot convert bytes `[102, 103, 104]` to type `Int`
When removing var zuio = 0
from the Asd
struct, the encoding doesn't throw any error, but when decoded again, it results in 3 rows intead of 2:
[Asd(asd: "asd"), Asd(asd: "fgh"), Asd(asd: "bla")]
Only when removing the comma in the string passed to the Asd
constructor everything goes well.
I know this package is appropriately titled CSV, but has any thought been given to supporting delimiters other than ","?
I have a data set that I'm parsing right now that uses "\t" and I've modified the case statement in Parser.swift to be case 44,9:
rather than case 44:
. This isn't a great solution, but for the time being it works.
If you were interested in supporting this concept I would think abstracting the ascii code away would be a good idea. This could be done by either creating some sort of Delimiter
enum or adding a Character
parameter to the parse function.
Between versions 0.3.0 and 0.3.1, a few changes in the code were introduced that require Swift 4.2 (which was not released yet), such as Bool.toggle()
.
I think changes that require different Swift version should be done in major versions or at least minor versions, not patch versions, as swift update
happily fetched the version 0.3.1
as part of the regular update process, which then made the code uncompilable in the current stable version of Xcode/Swift.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.