Create CSV file to read from Internet - csv

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

Related

Reading decimal values from JSON

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 read CSV file from device storage in flutter

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);
}
}

How to read local json import in flutter?

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);

Convert csv to json using phython

This is my code and it convert successfully. However, when i import this json into firebase and it state that Invalid JSON files.
import csv
import json
csvfile = open('C:/Users/Senior/seaborn-data/Denver DatasetCleaning Finalize.csv', 'r')
jsonfile = open('C:/Users/Senior/seaborn-data/Denver DatasetCleaning Finalize.json', 'w')
fieldnames = ("OFFENSE_CODE ","OFFENSE_CATEGORY_ID","FIRST_OCCURRENCE_DATE","DATE","YEAR","MONTH","DAY","TIME","HOUR","MINUTE","INCIDENT_ADDRESS","GEO_LON","GEO_LAT","NEIGHBORHOOD_ID")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
json.dump(row, jsonfile)
jsonfile.write('\n')
each time json.dump is called it is outputting json. but several json strings concatenated together are not still json
what you maybe want to do is read the entire csv into a variable, then json.dump that

django upload file using JSON

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