1
!python evaluation/mpd2csv.py --mpd_path "data" --out_path "data"

#mpd2csv.py
parser = argparse.ArgumentParser(description="Convert MPD")
parser.add_argument('--mpd_path', default=None, required=True)
tracks_file = open(path.join(args.out_path, 'tracks.csv'), 'w', newline='', encoding='utf8')
tracks_writer = csv.writer(tracks_file)

with open("data/tracks.csv", encoding='utf8') as json_file:
    data=json_file.read()

error: ' json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)'

I want to change the tracks.csv file to a json file. But there's been an error. How can you solve it?

1 Answer 1

1

Here I have made a sample csv as your requirement. It looks like

sample dataframe by littinrajan

Below code will convert tracks.csv to tracks.json

Code:

#import required libraries
import pandas as pd
import json

#reading csv using pandas
tracks = pd.read_csv('tracks.csv')
#diplay csv
print(tracks)

#converting dataframe to json
json_data = tracks.to_json(orient='records')

#saving json data to the file tracks.json
with open('tracks.json','w') as json_file:
    json.dump(json_data, json_file)

You can read tracks.json by below code

Code:

#read json file
with open('tracks.json','r') as json_file:
    data = json_file.read()
json_output = json.loads(data)
print(json_output)

Output:

[{"tracknumber":1,"count":2},{"tracknumber":2,"count":4},{"tracknumber":3,"count":6},{"tracknumber":4,"count":8},{"tracknumber":5,"count":10}]

Validated output:

Validated json by littinrajan

I hope this would be helpfull.

Sign up to request clarification or add additional context in comments.

1 Comment

For validating json you can use Online JSON Validator. Link: jsonlint.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.