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 <filename>
```
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:
<records> <patient> <mrn>1111</mrn> <dob>9/1/75</dob> <firstName>Jonathan</firstName> <lastName>Beyers</lastName> <icd9_1>414.01</icd9_1> <icd9_2>508.2</icd9_2> </patient> <patient> <mrn>2222</mrn> <dob>8/5/70</dob> <firstName>Melissa</firstName> <lastName>Johnston</lastName> <icd9_1>508.0</icd9_1> <icd9_2>485</icd9_2> </patient> </records>
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"
}
}
]
}
I have modified your code to convert CSV to XML format.
Hope this helps you.
import csv
from lxml import etree
csvFile = 'C:/Users/vipul/Desktop/file.csv'
def parse_csv(csvFile):
csvData = csv.reader(open(csvFile), delimiter='\t')
header = next(csvData)
root = etree.Element('records')
for row in csvData:
prod = etree.SubElement(root,'patient')
for index in range(0, len(header)):
child = etree.SubElement(prod, header[index])
child.text = row[index]
prod.append(child)
result = etree.tostring(root, pretty_print=True)
print(result)
parse_csv(csvFile)
OUTPUT:

The way I understand it is i'm trying to link a list that I read into...
Python with Pandas dataframe
I have a csv file that contains a large number of columns and
rows. I need to write a script that concatenates some elements of
the first row with some elements of the 2 row. Something like # if
data[1][0] starts with ch then concatenate the element right below
it. I have attached a picture of just a sample of my data. The
booleans have to stay on there as is. But I must drop the...
PYTHON 3 Object Oriented Programming
***a9q3.py file below***
class GradeItem(object):
# A Grade Item is anything a course uses in a grading scheme,
# like a test or an assignment. It has a score, which is assessed by
# an instructor, and a maximum value, set by the instructor, and a weight,
# which defines how much the item counts towards a final grade.
def __init__(self, weight, scored=None, out_of=None):
"""
Purpose:
Initialize the GradeItem object.
Preconditions:
:param weight: the weight...
Reading and parsing a CSV file in Java
NOTE:
a.) The first row contains the field definition
b.) Columns are separated by comma
This is the data.csv file
These are the coding instructions. Questions 1,2 and 3
This is my code so far
FirstName Radioactive Man LastName DateOfBirth SSN Salary Role Zip Phone garlic 9/29/1912 846330158 Administration 69989 39157 7166875260 Mockingbird Captain Triumph Deathstroke, th persimmon 9/22/1956 835340509 Administration 13884 39157 1421813391 usb 7/19/1940 8/8/1970 979204716 Back Office 75710 39157...
Reading and parsing a CSV data file in java
Note:
I.) the first row contains the field definition
II.) Columns are separated by comma
This is the data.csv file
These are the instructions
This is my code so far
A D F G H J K 1 FirstName LastName DateOfBirth SSN Role Salary Zip Phone 2 Radioactive Man BMockingbird 4Captain Triumph 5 Deathstroke, th Chief garlic 9/29/1912 846330158 Administration 39157 7166875260 69989 persimmon 9/22/1956 835340509 Administration 13884 39157 1421813391 usb...
Reading and parsing a CSV data file in java
Note:
I.) the first row contains the field definition
II.) Columns are separated by comma
This is the data.csv file
These are the instructions
This is my code so far
A D F G H J K 1 FirstName LastName DateOfBirth SSN Role Salary Zip Phone 2 Radioactive Man BMockingbird 4Captain Triumph 5 Deathstroke, th Chief garlic 9/29/1912 846330158 Administration 39157 7166875260 69989 persimmon 9/22/1956 835340509 Administration 13884 39157 1421813391 usb...