473,289 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 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 11059
dev7060
626 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

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

Similar topics

5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
8
by: kathy | last post by:
I can not find something like atof() which can convert string to double. Can anyone help me?
4
by: Akihiro KAYAMA | last post by:
Hi all. I would like to ask how I can implement string-like class using tuple or list. Does anyone know about some example codes of pure python implementation of string-like class? Because I...
3
by: Ursula | last post by:
Is it possible to convert a string in a file. The problem is this: I have an object string that is a file xml and I want to pass to Deserialize function, but Deserialize function expect an object...
3
by: ravibantu | last post by:
Hi guys, I am a newbie to python. Does anybody know how i can read a dat file into python and convert into xcel format? Thank you ravi
7
by: musclemilk | last post by:
Good day, now i know that someone was having the same error message and i read the thread but didnt help me out too much! so basically i have a command button that on a form that needs to open...
15
by: shiniskumar | last post by:
How to convert string to double? Ive got a double variable dTot; its value is 5.037717235E7 when i did FreemarkerTools.formatDecimal(dTot) i got it as string "50377172.35". now i want to...
1
by: foocc | last post by:
Hi, im trying to convert data reports (.dsr) in vb6 to crystal reports in vb.net. However, I cant seems to find it in my Microsoft Visual Studio 2005 Tools for Office. I've tried going to...
3
by: Brandon | last post by:
How do I convert a string to a char array? I am doing this so I can edit the string received from an sql query so I can remove unnecessary characters.
3
by: Chocolade | last post by:
Im using win32_printer and im adding to listbox1 a list of the printer properties. but then when im scrolling down in the listbox1 and getting to the last property i see the list of properties start...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.