Question
The way I understand it is i'm trying to link a list that I read into python from a cvs file to json and xml
The way I understand it is i'm trying to link a list that I read into python from a cvs file to json and xml and pass the doctest. Please refere the lines where I show what I did below.
home / study / engineering / computer science / questions and answers / """this script converts a csv file with headers ...
Question: """This script converts a CSV file with headers to...
Bookmark
"""This script converts a CSV file with headers to XML or JSON, depending on the command line argument supplied. This script will be executed by calling: ``` python src/lab1.py xml/json``` Output of XML and JSON should be printed to stdout. """ # Use argparse or argv to parse command line arguments. import argparse # https://docs.python.org/2/library/argparse.html from sys import argv #https://docs.python.org/2/library/sys.html#sys.argv import csv import xml import json def parse_csv(filename): """Parse a CSV file by separating it into headers and additional data. Parameters ---------- filename : str A path to a CSV file. Returns ------- (list, list) A tuple containing two lists: 1. The first list should contain the headers from the CSV file. If the headers are "first_name, last_name, dob" then this will be ["first_name", "last_name", "dob"]. 2. The second list should contain the data in the CSV file as a list of lists. For example, if there are two rows in the CSV file "1, 2, 3," and "4, 5, 6" then this list would look like [[1, 2, 3], [4, 5, 6]]. """ pass ################################### # Create your other functions here. ################################### if __name__ == "__main__": # Parse command line arguments to convert CSV to either XML or JSON. pass
i have already finished the first function parse_csv
def parse_csv(filename):
new_file = open(filename, "r") csv_reader = csv.reader(new_file) file_header= csv_reader.next() new_lst = [] for row in csv_reader: new_lst.append(row)
return (file_header,new_lst)
now i want to wite function to convert it to xml
def covert_2_xml(list):
and another function to convert it to json
def convert_2_json(list):
i have to hard copy only records and patient and all the csv file information will be given when i call the functions later passing the file name as a parameter.
here is the sample input file:
mrn,dob,firstName,lastName,icd9_1,icd9_2 1111,9/1/75,Jonathan,Beyers,414.01,508.2 2222,8/5/70,Melissa,Johnston,508.0,485
the xml output should be like:
1111 9/1/75 Jonathan Beyers 414.01 508.2 2222 8/5/70 Melissa Johnston 508.0 485
and the json output should be like:
{ "records":[ { "patient":{ "mrn":"1111", "dob":"9/1/75", "firstName":"Jonathan", "lastName":"Jonathan", "icd9_1":"414.01", "icd9_2":"508.2" } }, { "patient":{ "mrn":"2222", "dob":"8/5/70", "firstName":"Melissa", "lastName":"Johnston", "icd9_1":"508.0", "icd9_2":"485" } } ] }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started