Comments (5)
Hi, @Phalaen. Thank you for your feedback.
Under the hood, both to read from String
and InputStream
are the same in terms of parsing logic.
So, can you give me a reproductive program like the below code? (The below code correctly works.)
fun main() {
val data = """Device serial,Date ,Temperature 51 (Medium) °C
|11869, 2021-02-09 00:14:59, 7.2""".trimMargin()
val rows = csvReader().readAll(data)
println(rows) // Result: correctly parsed
}
from kotlin-csv.
Dear @doyaaaaaken,
thank you, this is my code:
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.*
import androidx.lifecycle.lifecycleScope
import java.net.MalformedURLException
import java.net.URL
import com.github.doyaaaaaken.kotlincsv.dsl.csvReader
class MainActivity : AppCompatActivity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
// url to download json data
val url:URL? = try {
URL(" hidden ")
}catch (e:MalformedURLException){
Log.d("Exception", e.toString())
null
}
lifecycleScope.launch(Dispatchers.IO){
url?.getStream()?.apply {
// default dispatcher for json parsing, cpu intensive work
withContext(Dispatchers.Default){
// val list = parseDOMNesaTempIst(this@apply)
val list = parse(this@apply)
// main dispatcher for interaction with ui objects
withContext(Dispatchers.Main){
textView.append("\n\nReading data from website....\n")
textView.append(list.toString())
}
}
}
}
}
}
}
// extension function to get string data from url
fun URL.getStream(): InputStream? {
val stream = openStream()
return stream
}
data class DataClass(
val FirstColumn: String,
val SecondColumn: String,
val ThirdColumn: String )
fun parse(data:InputStream): List?{
val list = mutableListOf<DataClass>()
try {
val rows: List<List<String>> = csvReader().readAll(data)
for (i in rows) {
var firstColumn : String = i[0]
var secondColumn : String = i[1]
var thirdColumn : String = i[2]
list.add(DataClass(FirstColumn =firstColumn, SecondColumn = secondColumn, ThirdColumn = thirdColumn))
}
}
catch (e: Exception) {
e.printStackTrace()
}
return list
}
In R.layout.activity_main there is only a button ("button") and a textview ("textView").
Thank you
from kotlin-csv.
@Phalaen
I cannot reproduce that in my environment.
Can you check what the InputStream content actually is?
In your code, you can debug it just before val rows: List<List<String>> = csvReader().readAll(data)
line.
Hint: You can convert InputStream into String by using the below code.
val inputStream: InputStream = TODO()
val csvContent: String = inputStream.bufferedReader().use(BufferedReader::readText)
from kotlin-csv.
Dear @doyaaaaaken ,
I tried converting inputstream into string and I think the problem is aready here, as the string is only partial:
Device serial,Date ,Rainfall 10 (Accumulated) mm
11869,2021-02-20,0
11869,2021-02-21,0
11869,2021-02-22,0
11869,2021-02-23,0
11869,2021-02-24,0.2
11869,2021-02-25,0
11869,2021-02-26,0
11869,2021-02-27,0
Is it normal? This never happened before to me
from kotlin-csv.
Solved!
Using the string text (val csvContent: String = inputStream.bufferedReader().use(BufferedReader::readText) ) parsing is correctly performed.
Thank you @doyaaaaaken !
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
- Only reads 23 instead of 614 lines HOT 1
- Add reader's option to skip header row
- 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.