472,131 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,131 software developers and data experts.

Python: convert CSV string to JSON format

2 2Bits
# 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_last_name = input("Enter author last name: ")
book_publisher = input("Enter book publisher: ")
year_published = input("Enter year published: ")
book_price = float(input("Book price: "))

book_title = book_title.strip()
book_ISBN = book_ISBN.strip()
book_author_last_name = book_author_last_name.strip()
book_publisher = book_publisher.strip()
year_published = year_published.strip()

return f"%s/%s/%s/%s/%s/%.2f"%(book_title,book_ISBN,book_author_last_name, book_publisher,year_published,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(string_data):

string_to_csv_format = "".join(string_data)
return string_to_csv_format.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_string_to_json):

string = csv_formatted_string_to_json.replace(",",":")
return string
Oct 12 '21 #1
3 9668
dev7060
624 Expert 512MB
What's the question?
Oct 19 '21 #2
sergioITA
2 2Bits
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?
Oct 19 '21 #3
Vanisha
25 16bit
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,city
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.splitlines())
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.
Feb 20 '23 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Haoyu Zhang | last post: by
8 posts views Thread by kathy | last post: by
4 posts views Thread by Akihiro KAYAMA | last post: by
3 posts views Thread by Ursula | last post: by
3 posts views Thread by Brandon | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.