473,416 Members | 1,797 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,416 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 11082
dev7060
636 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.