close2 / csv Goto Github PK
View Code? Open in Web Editor NEWA dart csv to list codec / converter
License: MIT License
A dart csv to list codec / converter
License: MIT License
I would be great to see a null safety version published.
The current Dart SDK version is 2.1.0.
Because csv 4.0.0 requires SDK version >=2.0.0-dev.0.0 <2.1.0 and no versions of csv match >4.0.0 <5.0.0, csv ^4.0.0 is forbidden.
Csv has an optional parameter at the header line.
For example
"sep=;"
a;b;
1;2;
3;4;
It is a csv which represents:
a | b |
---|---|
1 | 2 |
3 | 4 |
But when i use the csv to parse it, i fould the result list which contain the first line "sep=;"
, and also the empty column will be included.
The result list will be:
list[0]=["sep=;"]
list[1]=["a","b",""]
list[2]=[1,2,""]
list[3]=[3,4,""]
Is there are any optional parameter to assign the sep parameter? I current use csv which version number is '5.0.2'.
So I was testing CSV documents in flutter and when debugging I get a List of Lists (a list for each row) as you can see in the attachment, but when deploying I get a different result: A List of a List with every cell value (also attached).
My code:
Future<void> loadCSV() async { final _rawData = await rootBundle.loadString("assets/test.csv"); List<List<dynamic>> listData = const CsvToListConverter().convert(_rawData); js.context.callMethod("alert", <String>["listData $listData"]); }
Build command:
if cd flutter; then git pull && cd ..; else git clone https://github.com/flutter/flutter.git; fi && flutter/bin/flutter config --enable-web && flutter/bin/flutter build web --release
Hi There,
Thank you for the package.
I tried to parse some csv file, but looks like there are something not working well.The numbers or timedate are deliminated with "":
the code:
// Read the file
final strFile = await file.readAsString(
encoding: systemEncoding,
);
List<List<dynamic>> listLines = const CsvToListConverter(
eol: "\n",
fieldDelimiter: ",",
shouldParseNumbers: false,
).convert(strFile);
The data in csv file:
"Measurement position,,,50,150"
"Time stamp,2014-05-15,,14:21:25,14:21:25"
Thank you.
I ran a fresh pub get using Dart 1.17.1 and it can't find csv 3.0.1
$ dart --version
Dart VM version: 1.17.1 (Fri Jun 10 04:46:03 2016) on "macos_x64"
$ cat pubspec.yaml
name: "enchilada"
dependencies:
browser: "^0.10.0+2"
csv: ^3.0.1
$ pub get
Resolving dependencies...
Package csv has no versions that match >=3.0.1 <4.0.0 derived from:
- enchilada depends on version ^3.0.1
Because the Dart SDK's current version is 1.16.1, your project's dependency in this area is preventing me from including the CSV package.
Tested with this code:
import 'dart:io';
import 'dart:convert' show utf8;
import 'package:csv/csv.dart';
final csvFilePath = './addresses.csv';
void main(List<String> arguments) async {
final input = File(csvFilePath).openRead();
final raw = await input
.transform(utf8.decoder)
.transform(CsvToListConverter())
.toList();
print(raw);
}
and this dataset: "addresses.csv"
John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
In the console output you can see that "08075
Jack" are the same element in the resulting List
[[John, Doe, 120 jefferson st., Riverside, NJ, 08075
Jack, McGinnis, 220 hobo Av., Phila, PA, 09119
John Da Man, Repici, 120 Jefferson St., Riverside, NJ, 8075]]
And the same as in the issue #49. All rows are a single element of the resulting List
Hi! Does the library supports UTF-8 characters? I tried to create a .csv-file with cyrillic symbols without success.
I also get :
"A value of type 'dynamic' can't be assigned to a variable of type 'String'."
In : lib/csv_settings_autodetection.dart
Originally posted by @926 in #6 (comment)
stream type is List not List.
We might have to abandon being a codec.
'package:csv/csv_to_list_converter.dart': malformed type: line 8 pos 34: cannot resolve class 'StreamTransformerBase' from 'CsvToListConverter'
class CsvToListConverter extends StreamTransformerBase<String, List>
I am using the CSV plugin to read data from existing CSV, When I try to pick a CSV file and perform CsvToListConverter
it gives all CSV data as a single item in the list on behalf of separate rows in the list...
I am using examples given in the package itself to read CSV.
You can test your example with this CSV: https://figshare.com/articles/dataset/temperature_csv/3171766
I think this is related to #15 . When tests are run with dart --preview-dart-2
(with a recent dev build of Dart, like at least 2.0.0-dev.50.0), we get some runtime errors. Here are some examples:
$ dart --preview-dart-2 test/list_to_csv_test.dart
...
00:00 +11 -1: Works as transformer [E]
type '_GeneratedStreamImpl<List<dynamic>>' is not a subtype of type 'Stream<List<List<dynamic>>>' of 'stream' where
_GeneratedStreamImpl is from dart:async
List is from dart:core
Stream is from dart:async
List is from dart:core
List is from dart:core
dart:convert/converter.dart Converter.bind
dart:async/stream.dart 630:30 Stream.transform
test/list_to_csv_test.dart 186:28 main.<fn>
...
00:00 +11 -1: Issue 5. Quote correctly
00:00 +12 -1: Issue 7. Test delimitAllFields
00:00 +13 -1: Some tests failed.
Also
$ dart --preview-dart-2 test/csv_to_list_test.dart
...
00:00 +21 -7: Transformer autodetects settings for a multiline csv correctly [E]
type 'List<dynamic>' is not a subtype of type 'List<List<dynamic>>' of 'data' where
List is from dart:core
List is from dart:core
List is from dart:core
dart:async/stream_transformers.dart _EventSinkWrapper.add
lib/csv_to_list_converter.dart 242:18 CsvToListSink._add
lib/csv_to_list_converter.dart 251:5 CsvToListSink.ad
...
00:00 +21 -7: Argument verification works
00:00 +22 -7: Some tests failed.
Trying to parse a CSV file that ends with "
but without the hidden EOL char like \r\n
after "
, it will result in an error: The text end delimiter (") for the last field is missing.
.
It seems related to the _insideQuotedString
flag maintenance during the parse, which will be reset only when
after it was set to true.
The issue here does not meet these two conditions and the flag stays true to parse closing and leads to the error.
is it available to create multi-sheet in csv file ,
it can do in Numbers(MacOS)
This library does not seem to be compatible with Dart 1.22. It prevents a pub build in a project depending on it with analyzer errors.
It seems that extending Converter and implementing StreamTransformer is the problem. Removing the implements StreamTransformer in both list_to_csv_converter.dart and csv_to_list_converter.dart seem to resolve the issue since a Converter already implements StreamTransformer.
abstract class Converter<S, T> implements StreamTransformer<S, T> {
class ListToCsvConverter extends Converter<List<List>, String>
implements StreamTransformer {
Analysis Errors:
➜ csv git:(master) ✗ dartanalyzer lib
Analyzing [lib]...
[error] Inconsistent declarations of 'bind' are inherited from (Stream<String>) → Stream<List<List<dynamic>>>, (Stream<dynamic>) → Stream<dynamic>. (/Users/robbecker/code/csv/lib/csv_to_list_converter.dart, line 8, col 7)
[error] Base class introduces an invalid override. The type of 'Converter.bind' ('(Stream<String>) → Stream<List<List<dynamic>>>') isn't a subtype of 'StreamTransformer<dynamic, dynamic>.bind' ('(Stream<dynamic>) → Stream<dynamic>'). (/Users/robbecker/code/csv/lib/csv_to_list_converter.dart, line 8, col 26)
[error] Inconsistent declarations of 'bind' are inherited from (Stream<List<List<dynamic>>>) → Stream<String>, (Stream<dynamic>) → Stream<dynamic>. (/Users/robbecker/code/csv/lib/list_to_csv_converter.dart, line 74, col 7)
[error] Base class introduces an invalid override. The type of 'Converter.bind' ('(Stream<List<List<dynamic>>>) → Stream<String>') isn't a subtype of 'StreamTransformer<dynamic, dynamic>.bind' ('(Stream<dynamic>) → Stream<dynamic>'). (/Users/robbecker/code/csv/lib/list_to_csv_converter.dart, line 74, col 26)
[error] Invalid override. The type of 'ListToCsvConverter.startChunkedConversion' ('(Sink<dynamic>) → List2CsvSink') isn't a subtype of 'Converter<List<List<dynamic>>, String>.startChunkedConversion' ('(Sink<String>) → Sink<List<List<dynamic>>>'). (/Users/robbecker/code/csv/lib/list_to_csv_converter.dart, line 173, col 3)
5 errors found.
How can I create another sheet for the same file?
[ '1', 3 ] should result in '"1",3'
Currently 1 is not quoted.
Far too many documents use the system eol encoding.
It shouldn't be a problem to accept both.
using csv: ^4.0.3
I have successfully converted a csv file on Andoid, but the same code doesn't work on iOS.
String codetxt = await rootBundle.loadString('assets/file.csv');
List<List<dynamic>> codeList = CsvToListConverter().convert(codetxt);
print('codelist size ' + codeList.length.toString());
This results in 66k entries for Android, but only 1 for iOS.
I've checked that the csv is loading properly on iOS. I'm not sure what else I should be looking for to track down the cause?
Could it be possible to use RegEx
field delimiters? I would like to use multiple tabs as delimiters.
I am using this library to generate the csv from array list in flutter web
When there is chrome works fine.
But in safari it gives me the csv data but not in different raw but in all the records in single row
If anyone has solution please provide
Thanks in advance
First of all, thanks for the library, very helpful.
I'm writing string values, and I even have them typed as such, but the library is deciding when to quote them, this is causing a string value of "1973" to be attempted to read as a number which fails as I'm trying to read it as a string again. So, right not I can't read the same file the library wrote.
Let me know if I'm using it incorrectly, my current workaround is to tell it to not decode any numbers, but that doesn't let me have numeric columns.
I'd appreciate it if I could tell the encoder to always encode strings with quotes, and leave numeric types w/o quotes. Barring that, the next best thing would be to specify on encode/decode how to encode each column.
Hi,
Thanks for providing this package. I've tried to read in a large csv file 165,000 rows, 20 colums about 30 MB of data. It took more than 1 min to convert the file into a List<List>.
Stopwatch stopwatch = new Stopwatch()..start();
var str = new File("testfile.csv").readAsStringSync(); // the read part is very fast
List<List> aux = const CsvToListConverter(eol: "\n").convert(str);
print("Took ${stopwatch.elapsed} to read csv file");
This is with Dart 1.8.5.
For comparison, R reads the data into a data.frame in less than 5 seconds.
Is there anything that can be done to speed up the Dart implementation?
Thanks,
Tony
Getting error -
error: Undefined name 'csvCodec'. (undefined_identifier at [easy_csv] example\exa.dart:10)
I implemented decoder example from dart csv package like -
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:csv/csv.dart';
main() async {
final input = new File('foo.csv').openRead();
final fields =
await input.transform(utf8.decoder).transform(csvCodec.decoder).toList();
}
Hi,
Thank you for your hardword to make a great lib.
Currently, I found an issue with following csv content:
"A B", "C, D"
I think the result should be two fields: A B
& C, D
.
But actually, the lib parse result as three fields: A B
, C
and D
;
Here is my sample code:
import 'package:csv/csv.dart';
void main() {
final rows = const CsvToListConverter().convert('"A B", "C, D"\r\n');
for (final row in rows) {
print(row.join('_ '));
}
}
Output
A B_ C_ D
getCsv() async {
String appDocDir = "";
await getTemporaryDirectory().then((onValue) {
appDocDir = onValue.path;
});
final res = const ListToCsvConverter().convert([
['aaa', ',', 'ccc'],
['barra n \n']
]);
File f = new File(appDocDir + "b.csv");
f.writeAsString(res);
}
output:
I/flutter (13463): File: '/data/user/0/com.drodriguez.my_rents/cacheb.csv'
I'm trying to use csv-3.0.1 to parse some csv files. The conversion from CSV to List is ok, but I'm experiencing a strange behavior when converting a list to CSV. The problem is that the converter does not produce the text Delimiter, even though I try to force it. Here is one example:
$ cat test.dart
import 'package:csv/csv.dart';
var csvStr = '''
"Alice","Austria",1
"Bob",",Brazil",2
''';
main() {
var rowsAsLists = const CsvToListConverter(eol: '\n').convert(csvStr);
print(rowsAsLists);
var listsAsRows = const ListToCsvConverter().convert(rowsAsLists);
print(listsAsRows);
listsAsRows = const ListToCsvConverter().convert(rowsAsLists, textDelimiter: '#');
print(listsAsRows);
}
$ dart test.dart
[[Alice, Austria, 1], [Bob, ,Brazil, 2]]
Alice,Austria,1
Bob,",Brazil",2
Alice,Austria,1
Bob,#,Brazil",2
Look that the delimiter was shown when the string begins with the field separator.
I'm using...
Dart VM version: 1.14.0 (Thu Jan 28 09:20:03 2016) on "macos_x64"
onPressed: () async {
//blocs.homePageBloc.onClickScheduleReview();
//Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) => ScheduleReviewPage()));
Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.storage]);
bool isShown = await PermissionHandler().shouldShowRequestPermissionRationale(PermissionGroup.storage);
String appDocDir;
await getTemporaryDirectory().then((onValue) {
appDocDir = onValue.path;
});
final res = const ListToCsvConverter().convert([['aaa', ',', 'ccc'], ['barra n \n']]);
print(greenPen("[HomePage] tempDir: " + appDocDir));
File f = new File(appDocDir + "b.csv");
f.writeAsString(res);
blocs.providers.databaseProvider.uploadFile(f, 'csv', "c", "nnn");
},
Is the output right? Tried to follow the official example.
Edit: solved by using ';' but still have a problem with ','
Edit: solved by using field delimiter ';'
I'm wondering if there is a reason that List<List>
is used across the project instead of List<List<String>
.
Current dart version is 2.1.2
CsvToListConverter adds extra rows for empty newlines at end of file.
Would it make sense to skip these empty rows?
I fixed it by doing:
rows.removeWhere((row) => row.length == 1 && row.first == '');
EDIT: or just call trim()
on String ..
I have a csv on this format with 28 lines:
id,name,image
familia,Família,familia.jpg
minhafamilia,Minha Família,minha-familia.jpg
escola,Escola,escola.jpg
sentimentos,Sentimentos,sentimentos.jpg
On android it creates a list of 28 itens, each one with 3 itens, just as the csv.
On IOs it creates a list of 61 itens, ignoring the first field and adding the 2nd and 3rd field on a sequence. Weird. I`m 3 hours here trying to find out what the problem was.
my code:
final String categoriesCsvContent = await rootBundle.loadString('assets/csvs/$categoriesCsvFileName');
_categoriesCsvData = CsvToListConverter().convert(categoriesCsvContent);
I got the problem just now, it is the eol, so I just changed like this:
categoriesCsvContent = await rootBundle.loadString('assets/csvs/$categoriesCsvFileName');
categoriesCsvContent = categoriesCsvContent.replaceAll('\n','\r\n');
But it would be nice if the code could treat this automatically.
Is there any way to exclude header from csv file while converting it into list?
[DocumentManager] The view service did terminate with error: Error Domain=_UIViewServiceErrorDomain Code=1 "(null)" UserInfo={Terminated=disconnect method}
I just use this
final input = new File('a/csv/file.txt').openRead();
final fields = await input.transform(utf8.decoder).transform(new CsvToListConverter()).toList();
After I upgraded to version 5.1.0 I get this warning message
/C:/Software/dart/hosted/pub.dev/csv-5.1.0/lib/src/csv_parser.dart:517:17: Warning: Operand of null-aware operation '!' has type 'bool' which excludes null.
} else if (!quoted! && shouldParseNumbers) {
^
Not sure if you are aware of it.
Thanks,
T
RFC says, that we don't need to quote any text, which doesn't contain any "special" characters (delimiters, separator, eol).
Add an options to nevertheless quote everything.
This issue has been extracted from issue #5.
I haven't found an option to set the escape character, is there any option planned for this ? I'm trying to convert my apache CSV code, which supports a few more options for custom formats in the wild.
This is only noticeable with large datasets but once you have anything resembling a non-trivial CSV file, this package takes absurd amounts of time to decode or encode the files.
Is there a way to specify the delimiter for decimal number? for example:
1.23
or
1,23
Thanks
Hi,
Would you consider adding another parameter to ListToCsvConverter that will customize how nulls get treated? Currently, for [1, null, 3] you get 1,null,3. That is inconvenient if you import the output into a spreadsheet program as the nulls will stick out. I would prefer the ability to convert the nulls to empty strings or maybe something else. Of course, I can preprocess my data, but that requires another pass.
What do you think?
Thanks,
Tony
Generated csv is not recognized as csv mime type
hi , I am using csv 5.0.0 plugin, and try to export the csv file which contains Traditional Chinese to Flutter Web. After export the file , using notepad open the file is OK, however open in Excel the content will become (???????? ) . Can you help with this? Thanks
Sample Code as below:
`
import 'package:csv/csv.dart';
String fileName = "testFile.csv";
List<List> wholeFile = new List<List>();
wholeFile.add(["姓", "名", "性別"]);
wholeFile.add(["陳", "大文", "男"]);
String csvString = ListToCsvConverter().convert(wholeFile);
new html.AnchorElement(href: "data:text/plain;charset=utf-8,$csvString")
..setAttribute("download", "$fileName")
..click();
`
Is there is anyway I can make it all fields data to be in double quoted? Now fields with comma(,) only coming as double quoted.
For example:
Current output:
1,Sunisha,[email protected],"Abbeville, Louisiana"
Expected output:
"1","Sunisha","[email protected]","Abbeville, Louisiana"
pubspec:
csv: ^5.0.1
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.