I want to import data into firebase database from a CSV file in flutter. So I pick .CSV file from device using file picker. Now how can I read data from that file?
At first import file_picker and CSV package from dart packages. Than define the method pickFile() as i have given below. it will pick the file from device storage and after selection it will print data. pickFile() funtion shoulb be called to get result.
import 'package:file_picker/file_picker.dart';
import 'package:csv/csv.dart';
import 'dart:convert' show utf8;
pickFile() async {
FilePickerResult result = await FilePicker.platform.pickFiles();
if (result != null) {
PlatformFile file = result.files.first;
final input = new File(file.path).openRead();
final fields = await input
.transform(utf8.decoder)
.transform(new CsvToListConverter())
.toList();
print(fields);
}
}
Related
I'm trying to read a JSON file in my Angular project. The file is located in the assets folder. The file has decimal values like:
{
"valueA": 0.40000000000002,
"valueB": 23.99999999999999995
}
My problem is that the values I got from importing the file are rounded to:
{
"ValueA": 0.4
"ValueB": 25
}
Is there a way to load the JSON with the exact digits from the source? Or convert them to a string? Unfortunately I have no way to change the source to split the numbers at the dot or to save them as a string. I could edit it in the pipeline that seeds the data but to me that looks like a really messy solution.
Currently I import and use the JSON like this:
import MyJson from 'src/assets/MyJson.json'
export class MyService {
private myJson = Object.assign(MyJson);
public getFieldsIWant() {
return this.myJson.theFields.iWant;
}
}
The main problem, I think, is with the first line import {.... If I print the imported File, it already "converted" the decimal place. Is there any other way to import JSON Files in TS so that this doesn't happen (already tried the import via the httpClient, same result)?
You can use libraries like https://github.com/josdejong/lossless-json to replace JSON.parse
const fs = require('fs');
const LosslessJSON = require('lossless-json');
const fileContents = fs.readFileSync('./data.json', 'utf8');
let json = LosslessJSON.parse(fileContents);
How to create CSV file with an opportunity to read it from URL? For example: http://winterolympicsmedals.com/medals.csv
You can make use of the csv package. Add csv dependency to your pubspec.yaml file
csv: ^4.0.3
http:
import 'package:csv/csv.dart' as csv;
import 'package:http/http.dart' as http;
Future<List<List>> csvToList() async {
var csvfile= await http.get('url');
csv.CsvToListConverter converter= new csv.CsvToListConverter(
eol: '\r\n', fieldDelimiter: ','
);
List<List> listCreated= converter.convert(csvfile.body); // the csv file is converted to a 2-Dimensional list
return listCreated;
}
The csv object also provides a method that can be used to create a csv file from a list of lists:
csv.ListToCsvConverter(List<List<dynamic>> input) //accepts a List of Lists as input
I have a JSON file in the flutter directory, not in assets.
json_data.json:-
{
"foo" : "bar"
}
I want to read this JSON on different files.
like
myfile.dart:-
import "package:mypackage/json_data.json" as data;
import 'dart:convert';
var my_data = json.decode(data);
I am getting the error:-
The name 'data' refers to an import prefix, so it must be followed by '.'.
Try correcting the name to refer to something other than a prefix, or renaming the prefix.
What is the problem here? why can't I read JSON from local import in flutter?
You should look into loading assets in flutter. You can't simply import an arbitrary file. Importing is for source code/libraries.
You need to declare this file as an asset in your pubspec.yaml
flutter:
assets:
- json_data.json
Then in your code you can load this asset as a String:
import 'package:flutter/services.dart' show rootBundle;
Future<String> getJson() {
return rootBundle.loadString('json_data.json');
}
You can decode the JSON with your existing code, but it should be placed in a method body somewhere. You call this getJson function to retrieve the JSON String:
var my_data = json.decode(await getJson());
Alternatively, you could simplify this even further by putting the contents of your JSON file directly into the code as a String, but this may not be possible, it depends on your intended use of this JSON.
const String data = '''
{
"foo" : "bar"
}
''';
var my_data = json.decode(data);
I am working with Flink streaming API and I want to continuously read CSV files from a folder, ignore the header and convert each row in the CSV file into a Java class (POJO). After all this processing, I should obtain a stream of Java objects(POJOs).
So far, I do the following to partially achieve the behavior(code below):
read the CSV files as regular text files, continuously
get a stream of strings from the CSV files
convert the stream of strings to a stream of Java objects
String path = "/home/cosmin/Projects/flink_projects/flink-java-project/data/";
TextInputFormat format = new TextInputFormat(
new org.apache.flink.core.fs.Path(path));
DataStream<String> inputStream = streamEnv.readFile(format, path, FileProcessingMode.PROCESS_CONTINUOUSLY, 100);
DataStream<MyEvent> parsedStream = inputStream
.map((line) -> {
String[] cells = line.split(",");
MyEvent event = new MyEvent(cells[1], cells[2], cells[3]);
return event;
});
However, with this I don't manage to remove the header line in each CSV file.
I have read that I can build a custom connector for reading CSV files by using createInput() or addSource () methods on the StreamExecutionEnvironment class.
Can you help with some guidance on how to achieve this, as I haven't found any examples beyond the Javadoc?
You could chain a filter function before your map function to filter out header lines
inputStream.filter(new FilterFunction<String>() {
public boolean filter(String line) {
if (line.contains("some header identifier")) return false;
else return true;
}
}).map(...) <Your map function as before>
i have JSON request like this:
object: { "fields":{ "src" : "http://dss.com/a.jpg", "data" : " //file is here" } }
i have the model like this:
class FileMy(models.Model):
f = models.FileField(upload_to='file_path/',)
How to save the file ?
You may use urllib to read the file and then you can add it to your model.
Take a look at this post:
Django: add image in an ImageField from image url
You may be able to wrap the data in a ContentFile which inherits from File and then save the file to the model directly.
from __future__ import unicode_literals
from django.core.files.base import ContentFile
from .models import FileMy
f1 = ContentFile("esta sentencia está en español")
f2 = ContentFile(b"these are bytes")
m1 = FileMy()
m2 = FileMy()
m1.f.save("filename", f1, save=True)
m2.f.save("filename", f2, save=True)
First of all, encode the raw data in the json request body.
from tempfile import NamedTemporaryFile
from django.core.files import File
def save_file_to_field(field, file_name, raw_content):
# field: reference to the model object instance field
img_temp = NamedTemporaryFile(delete=True)
img_temp.write(raw_content)
field.save(
file_name,
File(img_temp)
)
img_temp.flush()
What does this do:
creates a temporary file on your system that holds the data
uses the file field save method to trigger the usual file handling
deletes the temporary file