📜  bash python csv to json - Python (1)

📅  最后修改于: 2023-12-03 14:59:28.273000             🧑  作者: Mango

Bash Python CSV to JSON - Python

Are you tired of manually converting CSV files to JSON format? Look no further than this bash/python script! This script allows you to easily convert CSV files to JSON format with just a few simple steps.

Usage
  1. Download the following files to a directory on your machine:

    • csv_to_json_python.py
    • csv_to_json_bash.sh
  2. Open a terminal window and navigate to the directory where the above files are saved.

  3. Make the bash script executable by running the following command:

    chmod +x csv_to_json_bash.sh
    
  4. To convert a CSV file to JSON using the script, simply run the following command:

    ./csv_to_json_bash.sh path/to/csv/file.csv
    

    Where path/to/csv/file.csv is the relative or absolute path to the CSV file you would like to convert.

  5. The resulting JSON file will be saved in the same directory as the input CSV file, with the same filename but a .json extension.

How it Works

The csv_to_json_bash.sh script calls the csv_to_json_python.py script to actually perform the CSV to JSON conversion. The Python script uses the built-in csv module to read the CSV file, and then converts the data to a Python dictionary. Finally, the dictionary is converted to JSON format using the built-in json module.

Example

Consider the following example.csv file:

name,age,gender
Alice,25,Female
Bob,30,Male
Charlie,35,Male

To convert this file to JSON using the script, run the following command:

./csv_to_json_bash.sh example.csv

This will create a file called example.json in the same directory, with the following contents:

[
    {
        "name": "Alice",
        "age": "25",
        "gender": "Female"
    },
    {
        "name": "Bob",
        "age": "30",
        "gender": "Male"
    },
    {
        "name": "Charlie",
        "age": "35",
        "gender": "Male"
    }
]
Conclusion

The bash/python script presented here provides a simple and efficient method for converting CSV files to JSON format. With just a few simple steps, programmers can convert their data from one format to another without having to manually manipulate the data. Try it out today and see how it can streamline your workflow!