469,300 Members | 2,312 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,300 developers. It's quick & easy.

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
1 Week Ago #1
2 2735
dev7060
501 Expert 256MB
What's the question?
1 Days ago #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?
1 Days ago #3

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
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.