Comments (8)
Hi @jnfeinstein, thank for the question.
With the readNext()
method, you can read one row at a time.
https://github.com/doyaaaaaken/kotlin-csv#read-line-by-line
I think this feature is enough to deal with your proposed use-cases, isn't it?
from kotlin-csv.
@doyaaaaaken thanks for the timely reply. I'll attempt to use that and report back.
from kotlin-csv.
This line prevents the reader from being used in an asynchronous context as it is closing the stream on return. I believe the intended usage would be similar to:
val reader = csvReader().open(inputStream)
val nextRow = reader.readNext()
This allows the caller to take advantage of the state machine within the CSV reader while giving them the freedom to play with the input stream as desired.
In regards to headers, having fun readHeader(): List<String>
and fun readNextWithHeader(): Map<String, String>
would be helpful.
from kotlin-csv.
As I wrote on the principle section on README, I'd like to hide file close process.
So, if you could, I'd like you to use open
and readAllAsSequence
methods.
But, if there is the use-case to open and close files manually, we need to implement that feature.
On CSV writer, that use-case exists. So I provide openAndGetRawWriter
method to control file close process manually. The background is here. #59
What do you think whether the same structure is needed on CSV reader?
from kotlin-csv.
My use case is similar to #59 except from the read perspective.
on Java, we always need to close file. but it's boilerplate code and not friendly for non-JVM user.
In my opinion this makes sense for the methods that accept File
or fileName: String
as an input. I would expect that the library would handle opening and closing the file because anything else would be an unintended side-effect. However, an input of type InputStream
means that the user has manually opened the stream. The parallel expectation would be that the user is also expected to close the stream. It actually took some time debugging to determine that kotlin-csv
was doing this. The syntax would look like:
csvContent.toByteArrayOutputStream().use {
csvReader().open(it) {
doForEachRow(readNext())
}
doComplexOperationWithInputStream(it)
}
profit()
I've taken advantage of the same auto-close functionality used internally in kotlin-csv
to reduce boilerplate, but now have the capability to perform a complex operation on this stream that may be unique to my use case.
from kotlin-csv.
Actually, I partly agree with your below point. Thanks for your insightful opinion.
In my opinion this makes sense for the methods that accept File or fileName: String as an input. I would expect that the library would handle opening and closing the file because anything else would be an unintended side-effect. However, an input of type InputStream means that the user has manually opened the stream. The parallel expectation would be that the user is also expected to close the stream.
But, your providing example seems not to be possible because we can read InputStream
at most once.
In your example, you read InputStream twice.
On your use-case, you could write codes like a following snippet.
val ips: InputStream = "a1,b1\r\na2,b2".toByteArray().inputStream()
csvReader().open(ips) {
var row: List<String>? = readNext()
while(row != null) {
doComplexOperationWithInputStream(row)
row = readNext()
}
}
from kotlin-csv.
InputStream
is an abstract class, allowing the caller to create an implementation that behaves however they wish. Some of the standard implementations may even be reset. The overall point is that closing prematurely may be an undesirable side-effect to some calling applications. 😁
from kotlin-csv.
I can understand your opinion, but in my sense, to close file like below snippet are not unnatural.
The reason is written as code comment on below snippet.
val ips1: InputStream = "a1,b1\r\na2,b2".toByteArray().inputStream()
val csv1 = csvReader().readAll(ips1) // to close the InputStream, BECAUSE it was all read.
val ips2: InputStream = "a1,b1\r\na2,b2".toByteArray().inputStream()
val csv2 = csvReader().open(ips2) {
readAllAsSequence().toList()
} // to close the InputStream, BECAUSE it is enclosed by `open` method.
from kotlin-csv.
Related Issues (20)
- Make CsvFileReader.readNext() private HOT 6
- Keep input stream open HOT 2
- Optimize writeNext Method HOT 3
- The binary version of its metadata is 1.6.0, expected version is 1.4.2.
- Seeing a parsing error when quotes within text HOT 1
- Parser unable to parse csv file with lower row quantity compare with header HOT 6
- Introduce `insufficientFieldsRowBehaviour = "EMPTY_STRING"` option on CSV reader HOT 2
- How to read big csv file using your library?
- CsvFileWriter.writeRows writes line terminator even when passed list or sequence is empty HOT 3
- Allow an error interface channnel HOT 1
- Unable to parse with multiple quotes HOT 1
- Exception in thread "main" com.github.doyaaaaaken.kotlincsv.util.CSVParseFormatException: must appear escapeChar(") after escapeChar(") [rowNum = 729, colIndex = 467, char = "]
- Add support for WebAssembly HOT 2
- Use java.nio.Path instead of File
- Allow writing data with headers
- Make common ancestor for CsvReaderContext and CsvWriterContext
- Remove logger 3rd party library HOT 8
- java.lang.NoClassDefFoundError: com/github/doyaaaaaken/kotlincsv/dsl/CsvReaderDslKt HOT 1
- How to set/get Header HOT 1
- Write directly to a String HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlin-csv.