How do I fix my formatting.
I know how to get the header and can also get the data exported in json format out to file.
My problem is each column needs to have the item index for each line.
data = json.loads(response.text)
f = open("export-results.csv", "a", newline="")
writer = csv.writer(f)
header = 'Device Name', 'Operating System', 'IP Address'
for item in data:
writer.writerow(header)
writer.writerow(item['name'])
writer.writerow(item['os_version_and_architecture'])
writer.writerow(item['last_ip_address'])
f.close()
Each column to have the value in full formatting and chosen from the ITEM list.
You can use enumerate() to get index of the row. For example:
with open("export-results.csv", "w", newline="") as f:
writer = csv.writer(f)
header = "Index", "Device Name", "Operating System", "IP Address"
# write header
writer.writerow(header)
# write rows with index (starting from 1)
for idx, item in enumerate(data, 1):
writer.writerow(
[
idx,
item["name"],
item["os_version_and_architecture"],
item["last_ip_address"],
]
)
EDIT: Without the index:
with open("export-results.csv", "w", newline="") as f:
writer = csv.writer(f)
header = "Device Name", "Operating System", "IP Address"
# write header
writer.writerow(header)
# write rows
for item in data:
writer.writerow(
[
item["name"],
item["os_version_and_architecture"],
item["last_ip_address"],
]
)
Related
persons = [
{"name":"howard", "adress":"New Jersey", "blood group":"AB"},
{"name":"harry", "adress":"New York", "blood group":"O"},
]
output_file = "outputfile.tsv"
with open(outfilename, "w") as output:
for row in persons:
column_values = row.values()
line = "\t".join(column_values) + '\n'
output.write(line)
I tried using methods for csv but it didnt work furthermore I tried changing the dictionary but was not succesufull
Use csv module. In particular csv.DictWriter(). It can add the header using the dict keys as the field names and writeheader() to create the header. Then you write out the data using writerows().
import csv
persons = [
{"name":"howard", "adress":"New Jersey", "blood group":"AB"},
{"name":"harry", "adress":"New York", "blood group":"O"},
]
output_file = "outputfile.tsv"
with open(output_file, 'w') as csv_file:
hdr = persons[0].keys()
csvDictR = csv.DictWriter(csv_file, hdr, delimiter='\t')
csvDictR.writeheader()
csvDictR.writerows(persons)
cat outputfile.tsv
name adress blood group
howard New Jersey AB
harry New York O
I have made a CSV file where it stores a book, its author and the year it was published. I then made it where the program will display the file's data as a list to the user. I want to now ask the user to select a row and replace it with a different set of data. I then want this data back to the original CSV file, overwriting the existing data with the amended one. How can I do this?
Here is my code so far:
import csv
with open("Books.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["", "Book", "Author", "Year released"])
writer.writerow([0, "To kill a Mockingbird", "Harper Lee", "1960"])
writer.writerow([1, "A Brief History of Time", "Stephan Hawking", "1988"])
writer.writerow([2, "The Great Gatsby", "F.Scott Fitzgerald", "1922"])
writer.writerow([3, "The Man Who Mistook His Wife For a Hat", "Oliver Sacks", "1985"])
writer.writerow([4, "Pride and Prejudice", "Jane Austen", "1813"])
books = []
with open("books.csv", "r", newline="") as file2:
reader = csv.reader(file2)
for row in reader:
count, book, author, year_released = row
print(row)
Read the file in as a list. Then modify that list. Then write that list back to disk.
import csv
with open("Books.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["", "Book", "Author", "Year released"])
writer.writerow([0, "To kill a Mockingbird", "Harper Lee", "1960"])
writer.writerow([1, "A Brief History of Time", "Stephan Hawking", "1988"])
writer.writerow([2, "The Great Gatsby", "F.Scott Fitzgerald", "1922"])
writer.writerow([3, "The Man Who Mistook His Wife For a Hat", "Oliver Sacks", "1985"])
writer.writerow([4, "Pride and Prejudice", "Jane Austen", "1813"])
books = []
with open("books.csv", "r", newline="") as file2:
reader = csv.reader(file2)
books = list(reader)
print(*books, sep='\n')
line = input("select line: ")
title = input("title: ")
author = input("author: ")
year = input("year: ")
books[int(line) + 1] = [line, title, author, year]
with open("Books.csv", 'w', newline="") as file3:
writer = csv.writer(file3)
writer.writerows(books)
print(*books, sep='\n')
I have an excel file in which data is saved in csv format in such a way.This data is present in the excel file as shown below,under column A (The CSV File is generated by LabView Software code which i have written to generate data).I have also attached an image of the csv file for reference at the end of my question.
RPM,Load Current,Battery Output,Power Capacity
1200,30,12,37
1600,88,18,55
I want to create a Json file in such format
{
"power_capacity_data" :
{
"rpm" : ["1200","1600"],
"load_curr" : ["30","88"],
"batt_output" : ["12","18"],
"power_cap" : ["37","55"]
}
}
This is my code
import csv
import json
def main():
#created a dictionary so that i can append data to it afterwards
power_data = {"rpm":[],"load_curr":[],"batt_output":[],"power_cap":[]}
with open('power1.lvm') as f:
reader = csv.reader(f)
#trying to append the data of column "RPM" to dictionary
rowcount = 0
for row in reader:
if rowcount == 0:
#trying to skip the first row
rowcount = rowcount + 1
else:
power_data['rpm'].append(row[0])
print(row)
json_report = {}
json_report['pwr_capacity_data'] = power_data
with open('LVMJSON', "w") as f1:
f1.write(json.dumps(json_report, sort_keys=False, indent=4, separators=(',', ': '),encoding="utf-8",ensure_ascii=False))
f1.close()
if __name__ == "__main__":
main()
The output json file that i am getting is this:(please ignore the print(row) statement in my code)
{
"pwr_capacity_data":
{
"load_curr": [],
"rpm": [
"1200,30,12.62,37.88",
"1600,88,18.62,55.88"
],
"batt_output": [],
"power_cap": []
}
}
The whole row is getting saved in the list,but I just want the values under the column RPM to be saved .Can someone help me out with what I may be doing wrong.Thanks in advance.I have attached an image of csv file to just in case it helps
You could use Python's defaultdict to make it a bit easier. Also a dictionary to map all your header values.
from collections import defaultdict
import csv
import json
power_data = defaultdict(list)
header_mappings = {
'RPM' : 'rpm',
'Load Current' : 'load_curr',
'Battery Output' : 'batt_output',
'Power Capacity' : 'power_cap'}
with open('power1.lvm', newline='') as f_input:
csv_input = csv.DictReader(f_input)
for row in csv_input:
for key, value in row.items():
power_data[header_mappings[key]].append(value)
with open('LVMJSON.json', 'w') as f_output:
json.dump({'power_capacity_data' : power_data}, f_output, indent=2)
Giving you an output JSON file looking like:
{
"power_capacity_data": {
"batt_output": [
"12",
"18"
],
"power_cap": [
"37",
"55"
],
"load_curr": [
"30",
"88"
],
"rpm": [
"1200",
"1600"
]
}
}
I have a bunch of methods for creating CSV files that are starting to make me feel like I am really repeating myself. Here are a couple:
def genSmsOutputFile(self, smsDict):
with open(settings.MEDIA_ROOT + 'smsOutput.csv', 'w+') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow( ['電話番号','会員ID','TEL1','TEL2','TEL3','(ラベル無し)','(ラベル無し)','(ラベル無し)','(ラベル無し)','(ラベル無し)','(ラベル無し)','(ラベル無し)','(ラベル無し)','(ラベル無し)','(ラベル無し)','(ラベル無し)',] )
for key, value in smsDict.items():
phoneNumber = value[2] + value[3] + value[4]
writer.writerow( [ phoneNumber, value[0], value[2], value[3], value[4],'','','','','','','','','','','',''] )
return 'smsOutput.csv'
def genMailOutputFile(self, okMailMaga):
with open(settings.MEDIA_ROOT + 'mailMagaOutput.csv', 'w+') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow( [ '会員id','e-mail','お名前(姓)','お名前(名)','購入回数' ] )
for key, value in okMailMaga.items():
writer.writerow( [ value[0], value[1], value[2], value[3], value[4] ] )
return settings.MEDIA_ROOT + 'mailMagaOutput.csv'
I am working to create the function _writeOutputToFile(self, dictionary, columnNames):
But I am finding myself stuck on how I would write the line:
writer.writerow([value[0], value[1], value[2], value[3], value[4]])
Is there some way to grab an entire row of the CSV from a dictionary in one loop iteration dynamically?
You can concatenate lists to get a larger list. So as an example:
writer.writerow( [ phoneNumber, value[0], value[2], value[3], value[4],'','','','','','','','','','','',''] )
could be:
writer.writerow([phoneNumber, value[0]] + value[2:5] + [''] * 12)
I had a Python beginners course last year. Now I am trying to get a csv to json converter. I have searched quite some time and adapted and changed some of the code I found, until the output looked similar to what I want. I am using Python 3.4.2.
#kvorobiev this is an excerpt of my CSV, but it will do for the case. The first time Converting will work. After the second time you will see that the order of the headings will change within the json file.
The csv file looks like this
Document;Item;Category
4;10;C
What I am getting in the output file as of now (after applying the changes from kvorobiev):
[
{
"Item": "10",
"Category": "C",
"Document": "4"
};
]
The json string I want to get in the output file should look like:
[
{
"Document": "4",
"Item": "10",
"Category": "C"
},
]
You will notice the headings are in the wrong order.
Here is the code:
import json
import csv
csvfile = open('file1.csv', 'r')
jsonfile = open('file1.csv'.replace('.csv','.json'), 'w')
jsonfile.write('[' + '\n' + ' ')
fieldnames = csvfile.readline().replace('\n','').split(';')
num_lines = sum(1 for line in open('file.csv')) -1
reader = csv.DictReader(csvfile, fieldnames)
i = 0
for row in reader:
i += 1
json.dump(row, jsonfile, indent=4,sort_keys=False)
if i < num_lines:
jsonfile.write(',')
jsonfile.write('\n')
jsonfile.write(' ' + ']')
print('Done')
Thanks for helping.
Replace line
reader = csv.DictReader(csvfile, fieldnames)
with
reader = csv.DictReader(csvfile, fieldnames, delimiter=';')
Also, you open file1.csv and later get lines number from file.csv
num_lines = sum(1 for line in open('file.csv')) -2
Your solution could be reduced to
import json
import csv
csvfile = open('file1.csv', 'r')
jsonfile = open('file1.csv'.replace('.csv','.json'), 'w')
jsonfile.write('{\n[\n')
fieldnames = csvfile.readline().replace('\n','').split(';')
reader = csv.DictReader(csvfile, fieldnames, delimiter=';')
for row in reader:
json.dump(row, jsonfile, indent=4)
jsonfile.write(';\n')
jsonfile.write(']\n}')
If you want to save order of columns from csv you could use
from collections import OrderedDict
...
for row in reader:
json.dump(OrderedDict([(f, row[f]) for f in fieldnames]), jsonfile, indent=4)
jsonfile.write(';\n')
jsonfile.write(']\n}')