1

I need to convert data from my csv file to the one i am gonna use which is .js.

Lp.;Name;Surname;Desc;Unit;Comment
1;Jan;Makowski;Inf;km;
2;Anna;Nowak;Pts;km;Brak reakcji

If you can see column 'comment' does not always have record and I need to keep it that way. Also between data there is amount of tabs I need to set as well.

enter image description here

I've a file,i am working on right now but It show's me data in row like :

[{"Lp.;Name;Surname;Desc;Unit;Comment": "1;Jan;Makowski;Inf;km;"}, {"Lp.;Name;Surname;Desc;Unit;Comment": "2;Anna;Nowak;Pts;km;Brak reakcji"...]

I am new to python and I have no idea how to define what I need to get.

@@ Edit I managed to do that...

import json
import csv

# Deklaracja danych
fieldnames = ("Lp.", "Name", "Surname", "Desc", "Unit", "Comment")

#  Otwieranie plików
with open('file.csv', 'r', encoding = "utf8") as csvfile:
    reader = csv.DictReader(csvfile) # ,fieldnames)
    rows = list(reader)

# Zamykamy plik
csvfile.close()

# Tworzymy plik z danych
with open('file.json', 'w', encoding = "utf8") as jsonfile:
    json.dump(rows,jsonfile)
    # jsonfile.write(s.replace(';', '/t'))
# Zamykamy plik
csvfile.close()
3
  • Have a look at the builtin csv module, python2, python3. It allows you to have a list of fields for each row in the file. Commented May 22, 2017 at 12:08
  • yeah Iam watching it for a while, just cant understend how to build those rows and columns Commented May 22, 2017 at 12:22
  • Possible duplicate of Convert CSV to JSON (in specific format) using Python Commented May 22, 2017 at 13:20

5 Answers 5

5

I think this is your answer, this may not be the best way to do it, but it can gives you the result.

import csv
import json
with open('file.csv', 'r') as f:
    reader = csv.reader(f, delimiter=';')
    data_list = list()
    for row in reader:
        data_list.append(row)
data = [dict(zip(data_list[0],row)) for row in data_list]
data.pop(0)
s = json.dumps(data)
print (s)

output:

[{"Comment": "", "Surname": "Makowski", "Name": "Jan", "Lp.": "1", "Unit": "km", "Desc": "Inf"}, {"Comment": "Brak reakcji", "Surname": "Nowak", "Name": "Anna", "Lp.": "2", "Unit": "km", "Desc": "Pts"}]
Sign up to request clarification or add additional context in comments.

8 Comments

Iam getting wierd error "in <module> for row in reader:_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)" how u manage to get ur answer there?
I just copied your data to a file with csv extension.
may I know your python version ?
Iam using newest 3.6.1
@OskarGarczyński, Now you have the data, thats cool, now to make the view with this data is your job.
|
2

Pandas has both built-in .read_csv() and .to_json(). As an intermediate you get then a dataframe with which you can manipulate the data, including defining an index or data model.

import pandas as pd
df = pd.read_csv('file.csv')
# any operations on dataframe df
df.to_json('file.json')

1 Comment

+1. NB: by default to_json() will output column-wise. Alternatively, use the orient parameter to get row-wise output, e.g. df.to_json(..., orient='records').
2

You can minimize your script quite a lot given a few details of python:

import csv
import json

with open('file.csv', 'r', encoding='utf8') as csvfile:
    with open('file.json', 'w', encoding='utf8') as jsonfile:
        reader = csv.DictReader(csvfile, delimiter=';')
        json.dump(list(reader), jsonfile)

The details about why this works:

  1. Files opened as part of with-statements are closed automatically when the block is "done". (The corresponding PEP: https://www.python.org/dev/peps/pep-0343/)
  2. The list constructor can take Iterators (such as a csv.DictReader) as argument. (DictReader documentation: https://docs.python.org/3.7/library/csv.html#csv.DictReader)

This will create a list in memory with a dict for each row in the csv-file which might be worth keeping in mind if you are handling very large files. Sadly it isn't possible as far as I know to send the iterator directly to the json serializer without modifying the iterator or the serializer.

I'll leave the argument about which solution that is better from a maintenance and a readability perspective as an exercise for the reader.


If you're not reading from a local file on disk, but from another program via stdin you don't need to open the input file:

import csv
import json
import sys

with open('file.json', 'w', encoding='utf8') as jsonfile:
    reader = csv.DictReader(sys.stdin, delimiter=';')
    json.dump(list(reader), jsonfile)

The same goes for outputting to stdout via print() or json.dump(..., sys.stdout).

5 Comments

what if the csv is not in a file rather it is a hard code?
@Navi Ignoring the many whys regarding you have hardcoded csv in your code instead of a proper python structure, I recommnd looking ad io.StringIO (docs.python.org/3/library/…)
What I am asking is, I don't get the data from a csv file. Just from a source which prints in csv format. In this case how can I convert it into JSON format?@asheidan
@Navi That makes more sense but you haven't given enough details to be able to help you. 'prints in csv format' could be many things. If you are piping the data into your script from another program that prints it to stdout, remove the line that opens the csv-file and use sys.stdin as the file the DictReader should read from.
I think you didn't get my question. Is there anyway I could ask you like in another place?
1

Rather than writing a script, you could try the csvjson command line tool (written in Python):

http://csvkit.readthedocs.io/en/1.0.2/scripts/csvjson.html?highlight=csvjson

Comments

-1

A very quick example of solution:

import csv

output = []
with open('your_file.csv', 'rb') as csvfile:
    reader = csv.DictReader(csvfile)
    output = [item for item in reader]
json.dump(open('your_file.json'), output)

1 Comment

'r' is default mode in open. However Python docs use 'rb' mode(read binary) so I update answer accordingly