Hướng dẫn convert csv to json python - chuyển đổi csv sang json python

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọcfiles represent data in a tabular format, with several rows and columns. An example of a CSV file can be an Excel Spreadsheet. These files have the extension of .csv, for instance, geeksforgeeks.csv. In this sample file, every row will represent a record of the dataset, and each column will indicate a unique feature variable.

    Bàn luậnJSON (or JavaScript Object Notation) is a dictionary-like notation that can be used by importing the JSON package in Python. Every record (or row) is saved as a separate dictionary, with the column names as Keys of the dictionary. All of these records as dictionaries are saved in a nested dictionary to compose the entire dataset. It is stored with the extension .json, for example, geeksforgeeks.json

    Các tệp CSV (hoặc giá trị phân tách bằng dấu phẩy) biểu thị dữ liệu ở định dạng bảng, với một số hàng và cột. Một ví dụ về tệp CSV có thể là bảng tính Excel. Các tệp này có phần mở rộng của .csv, ví dụ, GeekSforGeeks.csv. Trong tệp mẫu này, mỗi hàng sẽ đại diện cho một bản ghi của tập dữ liệu và mỗi cột sẽ chỉ ra một biến tính năng duy nhất.

    • Mặt khác, JSON (hoặc ký hiệu đối tượng JavaScript) là một ký hiệu giống như từ điển có thể được sử dụng bằng cách nhập gói JSON trong Python. Mỗi bản ghi (hoặc hàng) được lưu dưới dạng từ điển riêng biệt, với tên cột là các khóa của từ điển. Tất cả các bản ghi này dưới dạng từ điển được lưu trong một từ điển lồng nhau để soạn toàn bộ bộ dữ liệu. Nó được lưu trữ với phần mở rộng .Json, ví dụ, geeksforgeek.json
    • Tham khảo các bài viết dưới đây để hiểu những điều cơ bản của JSON và CSV.

    Làm việc với dữ liệu JSON trong Python

    Làm việc với tệp CSV trong Python.

    Chuyển đổi CSV thành JSON

    Hướng dẫn convert csv to json python - chuyển đổi csv sang json python

    Python3

    Chúng tôi sẽ tạo một tệp JSON sẽ có một số từ điển, mỗi tệp đại diện cho một bản ghi (hàng) từ tệp CSV, với khóa như cột được chỉ định. & NBSP;

    Tệp CSV mẫu được sử dụng:

    NO___Trans___Pre___5 no___Trans___Pre___6

    NO___Trans___Pre___5 no___trans___pre___8

    pip install pandas
    
    1
    pip install pandas
    
    6
    pip install pandas
    
    7
    pip install pandas
    
    8
    pip install pandas
    
    3
    import pandas as pd
    df = pd.read_csv (r'Path where the CSV file is saved\File Name.csv')
    df.to_json (r'Path where the new JSON file will be stored\New File Name.json')
    
    0
    import pandas as pd
    df = pd.read_csv (r'Path where the CSV file is saved\File Name.csv')
    df.to_json (r'Path where the new JSON file will be stored\New File Name.json')
    
    1

    NO___Trans___Pre___9 no___Trans___Pre___10

    pip install pandas
    
    1no___Trans___Pre___12no___Trans___Pre___13 no___trans___pre___14

    import pandas as pd
    df = pd.read_csv (r'Path where the CSV file is saved\File Name.csv')
    df.to_json (r'Path where the new JSON file will be stored\New File Name.json')
    
    2no___Trans___Pre___23no___trans___pre___13 no___trans___pre___25

    import pandas as pd
    df = pd.read_csv (r'Path where the CSV file is saved\File Name.csv')
    df.to_json (r'Path where the new JSON file will be stored\New File Name.json')
    
    2no___Trans___Pre___27 no___Trans___Pre___28no___trans___pre___29 no___trans___pre___30

    pip install pandas
    
    1
    pip install pandas
    
    6
    pip install pandas
    
    7
    {"Product":{"0":"Desktop Computer","1":"Tablet","2":"Printer","3":"Laptop"},"Price":{"0":700,"1":250,"2":120,"3":1200}}
    
    4
    {"Product":{"0":"Desktop Computer","1":"Tablet","2":"Printer","3":"Laptop"},"Price":{"0":700,"1":250,"2":120,"3":1200}}
    
    5
    {"Product":{"0":"Desktop Computer","1":"Tablet","2":"Printer","3":"Laptop"},"Price":{"0":700,"1":250,"2":120,"3":1200}}
    
    6
    pip install pandas
    
    3
    import pandas as pd
    df = pd.read_csv (r'Path where the CSV file is saved\File Name.csv')
    df.to_json (r'Path where the new JSON file will be stored\New File Name.json')
    
    0
    {"Product":{"0":"Desktop Computer","1":"Tablet","2":"Printer","3":"Laptop"},"Price":{"0":700,"1":250,"2":120,"3":1200}}
    
    9

    import pandas as pd
    df = pd.read_csv (r'Path where the CSV file is saved\File Name.csv')
    df.to_json (r'Path where the new JSON file will be stored\New File Name.json')
    
    2import1
    pip install pandas
    
    3import3import4

    import pandas as pd
    df = pd.read_csv (r'C:\Users\Ron\Desktop\Test\Products.csv')
    df.to_json (r'C:\Users\Ron\Desktop\Test\New_Products.json')
    
    1no___Trans___Pre___32no___Trans___Pre___13 no___trans___pre___34no___trans___pre___35no___trans___pre___36

    import pandas as pd
    df = pd.read_csv (r'C:\Users\Ron\Desktop\Test\Products.csv')
    df.to_json (r'C:\Users\Ron\Desktop\Test\New_Products.json')
    
    1no___Trans___Pre___38no___Trans___Pre___13 no___trans___pre___40

    csv3

    Output:


    Bạn có thể sử dụng mẫu sau để chuyển đổi CSV thành chuỗi JSON bằng Python:

    import pandas as pd
    df = pd.read_csv (r'Path where the CSV file is saved\File Name.csv')
    df.to_json (r'Path where the new JSON file will be stored\New File Name.json')
    

    Tiếp theo, bạn sẽ thấy các bước để áp dụng mẫu trên trong thực tế.

    Bước 1: Chuẩn bị tệp CSV

    Chuẩn bị tệp CSV mà bạn muốn chuyển đổi thành chuỗi JSON.

    Ví dụ: hãy để chuẩn bị một tệp CSV (được gọi là ‘sản phẩm‘) có chứa các thông tin sau:Products‘) that contains the following information:

    Sản phẩm Giá bán
    Máy tính desktop700
    Máy tính bảng250
    Máy in120
    Máy tính xách tay1200

    Bước 2: Cài đặt gói Pandas

    Nếu bạn đã thực hiện như vậy, hãy cài đặt gói Pandas. Bạn có thể sử dụng cú pháp sau để cài đặt gói Pandas trong Windows:

    pip install pandas
    

    Bước 3: Chuyển đổi CSV thành Chuỗi JSON bằng Python

    Bây giờ bạn có thể sử dụng mẫu sau để hỗ trợ bạn trong quá trình chuyển đổi tệp CSV sang chuỗi JSON:

    import pandas as pd
    df = pd.read_csv (r'Path where the CSV file is saved\File Name.csv')
    df.to_json (r'Path where the new JSON file will be stored\New File Name.json')
    

    Ví dụ:

    • Đường dẫn mà tệp CSV được lưu là: C: \ Users \ Ron \ Desktop \ test \ Products.csvC:\Users\Ron\Desktop\Test\Products.csv
      • Trong đó ‘Sản phẩm là tên tệp và‘ CSV là tiện ích mở rộng tệpProducts‘ is the file name, and ‘csv‘ is the file extension
    • Đường dẫn nơi tệp JSON mới sẽ được lưu trữ là: C: \ Users \ ron \ Desktop \ test \ new_products.jsonC:\Users\Ron\Desktop\Test\New_Products.json
      • Trong đó ‘new_products là tên tệp mới và‘ json là tiện ích mở rộng tệpNew_Products‘ is the new file name, and ‘json‘ is the file extension

    Bạn cần phải sửa đổi các đường dẫn đến vị trí nơi các tệp sẽ được lưu trữ trên máy tính của bạn.

    Dưới đây là mã Python hoàn chỉnh để chuyển đổi tệp CSV thành chuỗi JSON cho ví dụ của chúng tôi:

    import pandas as pd
    df = pd.read_csv (r'C:\Users\Ron\Desktop\Test\Products.csv')
    df.to_json (r'C:\Users\Ron\Desktop\Test\New_Products.json')
    

    Chạy mã trong Python (được điều chỉnh theo đường dẫn của bạn) và tệp JSON mới sẽ được tạo tại vị trí được chỉ định của bạn.

    Nếu bạn mở tệp JSON, bạn sẽ thấy chuỗi sau:

    {"Product":{"0":"Desktop Computer","1":"Tablet","2":"Printer","3":"Laptop"},"Price":{"0":700,"1":250,"2":120,"3":1200}}
    

    Bạn cũng có thể muốn kiểm tra các hướng dẫn sau giải thích cách chuyển đổi:

    • Chuỗi JSON thành CSV
    • Excel đến CSV
    • CSV đến Excel

    Làm cách nào để chuyển đổi tệp CSV thành tệp JSON trong Python?

    Các bước để chuyển đổi CSV thành Chuỗi JSON bằng Python..
    Bước 1: Chuẩn bị tệp CSV.Chuẩn bị tệp CSV mà bạn muốn chuyển đổi thành chuỗi JSON.....
    Bước 2: Cài đặt gói Pandas.Nếu bạn chưa làm như vậy, hãy cài đặt gói Pandas.....
    Bước 3: Chuyển đổi CSV thành Chuỗi JSON bằng Python ..

    Bạn có thể chuyển đổi CSV thành JSON không?

    Phương pháp đầu tiên để chuyển đổi CSV thành JSON là một tiện ích miễn phí được cung cấp bởi một công ty có tên Retool.Retool chuyển đổi CSV sang JSON Utility rất đơn giản để sử dụng.Để chuyển đổi tệp của bạn, bạn có thể: Kéo và thả tệp CSV vào công cụ.. The Retool Convert CSV to JSON utility is very simple to use. To convert your file, you can: Drag and drop the CSV file into the tool.

    Làm cách nào để chuyển đổi tệp CSV thành JSON trong pyspark?

    Spark không cần bất kỳ gói hoặc thư viện bổ sung nào để sử dụng Parquet như, theo mặc định, được cung cấp với Spark.Tệp CSV được chuyển đổi thành tệp JSON bằng hàm "dataFrame.write.json (" path ")"."dataframe. write. json("path")" function.