import json
import csv

def json_to_csv(json_file):
	with open(json_file, 'r') as jsonfile, open('output.csv', 'w', newline='') as csvfile:
		jsn = json.load(jsonfile)
		fieldnames = []
		for name in jsn[0]:
			fieldnames += [name]
		writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

		writer.writeheader()
		for elem in jsn:
			writer.writerow(elem)

def csv_to_json(csv_file):
	with open(csv_file, 'r') as csvfile, open('output.json', 'w') as jsonfile:
		reader = csv.DictReader(csvfile)
		jsn = []
		for row in reader:
			jsn += [row]

		json.dump(jsn, jsonfile)
		
filename = input('Enter filename of CSV file: ')
csv_to_json(filename)
import json
import csv
import argparse

def json_to_csv(json_file):
	with open(json_file, 'r') as jsonfile, open('output.csv', 'w', newline='') as csvfile:
		jsn = json.load(jsonfile)
		fieldnames = []
		for name in jsn[0]:
			fieldnames += [name]
		writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

		writer.writeheader()
		for elem in jsn:
			writer.writerow(elem)

def csv_to_json(csv_file):
	with open(csv_file, 'r') as csvfile, open('output.json', 'w') as jsonfile:
		reader = csv.DictReader(csvfile)
		jsn = []
		for row in reader:
			jsn += [row]

		json.dump(jsn, jsonfile)

def main():
	parser = argparse.ArgumentParser()
	parser.add_argument('inputtype', help='The type of input', type=str, choices=['csv', 'json'])
	parser.add_argument('filename', help='Name of the input file', type=str)
	args = parser.parse_args()

	if (args.inputtype=='csv'):
		csv_to_json(args.filename)
		outputtype = 'json'
	elif (args.inputtype=='json'):
		json_to_csv(args.filename)
		outputtype = 'csv'

	print('[*] Output saved as output.{}'.format(outputtype))


if __name__=='__main__':
	main()
Add command-line arguments and main function
Add command-line arguments and main function

  import json
  import csv
+ import argparse
  
  def json_to_csv(json_file):
  	with open(json_file, 'r') as jsonfile, open('output.csv', 'w', newline='') as csvfile:
  		jsn = json.load(jsonfile)
  		fieldnames = []
  		for name in jsn[0]:
  			fieldnames += [name]
  		writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
  
  		writer.writeheader()
  		for elem in jsn:
  			writer.writerow(elem)
  
  def csv_to_json(csv_file):
  	with open(csv_file, 'r') as csvfile, open('output.json', 'w') as jsonfile:
  		reader = csv.DictReader(csvfile)
  		jsn = []
  		for row in reader:
  			jsn += [row]
  
  		json.dump(jsn, jsonfile)
- 		
- filename = input('Enter filename of CSV file: ')
+ 
+ def main():
+ 	parser = argparse.ArgumentParser()
+ 	parser.add_argument('inputtype', help='The type of input', type=str, choices=['csv', 'json'])
+ 	parser.add_argument('filename', help='Name of the input file', type=str)
+ 	args = parser.parse_args()
+ 
+ 	if (args.inputtype=='csv'):
- csv_to_json(filename)
+ 		csv_to_json(args.filename)
+ 		outputtype = 'json'
+ 	elif (args.inputtype=='json'):
+ 		json_to_csv(args.filename)
+ 		outputtype = 'csv'
+ 
+ 	print('[*] Output saved as output.{}'.format(outputtype))
+ 
+ 
+ if __name__=='__main__':
+ 	main()
Add command-line arguments and main function
## Code Before:
import json
import csv

def json_to_csv(json_file):
	with open(json_file, 'r') as jsonfile, open('output.csv', 'w', newline='') as csvfile:
		jsn = json.load(jsonfile)
		fieldnames = []
		for name in jsn[0]:
			fieldnames += [name]
		writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

		writer.writeheader()
		for elem in jsn:
			writer.writerow(elem)

def csv_to_json(csv_file):
	with open(csv_file, 'r') as csvfile, open('output.json', 'w') as jsonfile:
		reader = csv.DictReader(csvfile)
		jsn = []
		for row in reader:
			jsn += [row]

		json.dump(jsn, jsonfile)
		
filename = input('Enter filename of CSV file: ')
csv_to_json(filename)
## Instruction:
Add command-line arguments and main function
## Code After:
import json
import csv
import argparse

def json_to_csv(json_file):
	with open(json_file, 'r') as jsonfile, open('output.csv', 'w', newline='') as csvfile:
		jsn = json.load(jsonfile)
		fieldnames = []
		for name in jsn[0]:
			fieldnames += [name]
		writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

		writer.writeheader()
		for elem in jsn:
			writer.writerow(elem)

def csv_to_json(csv_file):
	with open(csv_file, 'r') as csvfile, open('output.json', 'w') as jsonfile:
		reader = csv.DictReader(csvfile)
		jsn = []
		for row in reader:
			jsn += [row]

		json.dump(jsn, jsonfile)

def main():
	parser = argparse.ArgumentParser()
	parser.add_argument('inputtype', help='The type of input', type=str, choices=['csv', 'json'])
	parser.add_argument('filename', help='Name of the input file', type=str)
	args = parser.parse_args()

	if (args.inputtype=='csv'):
		csv_to_json(args.filename)
		outputtype = 'json'
	elif (args.inputtype=='json'):
		json_to_csv(args.filename)
		outputtype = 'csv'

	print('[*] Output saved as output.{}'.format(outputtype))


if __name__=='__main__':
	main()