Python: convert CSV string to JSON format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sergioITA
    New Member
    • Oct 2021
    • 2

    Python: convert CSV string to JSON format

    # Create a file with the name data_format.py
    # Add the following functions to file data_format:
    # function get_book_info() , the function askes the user to enter the following information:
    # book title
    # book ISBN
    # book author last name
    # book publisher
    # year published
    # book price in Canadian dollars.
    # The function will eliminate leading and trailing spaces from title, ISBN, author name and publisher (use function strip ()).
    # The function will return a string from the given information in the same order specified above, separated by forward slash (/)
    # Note: price should be formatted to have two digits after the decimal point
    # Function to_csv_format() , the function takes one parameter which is the string generated by get_book_info() , parse it and return a string of the provided information in a csv format.
    # Function to_JSON_format( ), the function takes a CSV formatted string and returns the corresponding JSON format string. Use String Methods find() ,string slicing and string concatenation to implement the required functionality.
    # Create a main function as shown in the in-class lab section. Add function calls to get the book information, produce and display the csv

    def get_book_info() :
    book_title = (input("Enter the book title: "))
    book_ISBN = input("Enter book ISBN: ")
    book_author_las t_name = input("Enter author last name: ")
    book_publisher = input("Enter book publisher: ")
    year_published = input("Enter year published: ")
    book_price = float(input("Bo ok price: "))

    book_title = book_title.stri p()
    book_ISBN = book_ISBN.strip ()
    book_author_las t_name = book_author_las t_name.strip()
    book_publisher = book_publisher. strip()
    year_published = year_published. strip()

    return f"%s/%s/%s/%s/%s/%.2f"%(book_tit le,book_ISBN,bo ok_author_last_ name,book_publi sher,year_publi shed,book_price )


    # Function to_csv_format() , the function takes one parameter which is the string generated by get_book_info() ,
    # parse it and return a string of the provided information in a csv format.

    def to_csv_format(s tring_data):

    string_to_csv_f ormat = "".join(string_ data)
    return string_to_csv_f ormat.replace("/",",")



    #Function to_JSON_format( ), the function takes a CSV formatted string and
    #returns the corresponding JSON format string. Use String Methods find() ,
    #string slicing and string concatenation to implement the required functionality.

    def to_JSON_format( csv_formatted_s tring_to_json):

    string = csv_formatted_s tring_to_json.r eplace(",",":")
    return string
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    What's the question?

    Comment

    • sergioITA
      New Member
      • Oct 2021
      • 2

      #3
      so I need to convert the CSV formatted string into JSON format and I am having a hard time finding the solution for that. Can you please help?

      Comment

      • Vanisha
        New Member
        • Jan 2023
        • 26

        #4
        To convert a CSV string to JSON format in Python, you can use the built-in csv and json modules. Here is an example code that demonstrates how to do this:
        import csv
        import json
        csv_string = """name,age,cit y
        John,25,New York
        Alice,30,Los Angeles
        Bob,35,Chicago" ""

        # Read the CSV string into a list of dictionaries
        csv_data = []
        reader = csv.DictReader( csv_string.spli tlines())
        for row in reader:
        csv_data.append (row)

        # Convert the CSV data to JSON format
        json_data = json.dumps(csv_ data)
        print(json_data )

        If you're looking to improve your coding skills or learn new technologies, Cetpa Infotech offers a range of courses and training programs to help you stay up to date with the latest developments in the field. Check out our website for more information.

        Comment

        Working...