I want to convert CSV to JSON in python. I was able to convert simple csv files to json, but not able to join two csv into one nested json.
emp.csv:
empid | empname | empemail
e123 | adam | adam#gmail.com
e124 | steve | steve#gmail.com
e125 | brian | brain#yahoo.com
e126 | mark | mark#msn.com
items.csv:
empid | itemid | itemname | itemqty
e123 | itm128 | glass | 25
e124 | itm130 | bowl | 15
e123 | itm116 | book | 50
e126 | itm118 | plate | 10
e126 | itm128 | glass | 15
e125 | itm132 | pen | 10
the output should be like:
[{
"empid": "e123",
"empname": "adam",
"empemail": "adam#gmail.com",
"items": [{
"itemid": "itm128",
"itmname": "glass",
"itemqty": 25
}, {
"itemid": "itm116",
"itmname": "book",
"itemqty": 50
}]
},
and similar for others]
the code that i have written:
import csv
import json
empcsvfile = open('emp.csv', 'r')
jsonfile = open('datamodel.json', 'w')
itemcsvfile = open('items.csv', 'r')
empfieldnames = ("empid","name","phone","email")
itemfieldnames = ("empid","itemid","itemname","itemdesc","itemqty")
empreader = csv.DictReader( empcsvfile, empfieldnames)
itemreader = csv.DictReader( itemcsvfile, itemfieldnames)
output=[];
empcount=0
for emprow in empreader:
output.append(emprow)
for itemrow in itemreader:
if(itemrow["empid"]==emprow["empid"]):
output.append(itemrow)
empcount = empcount +1
print output
json.dump(output, jsonfile,sort_keys=True)
and it doesnot work.
Help needed. Thanks
Okay, so you have a few problems. The first is that you need to specify the delimiter for your CSV file. You're using the | character and by default python is probably going to expect ,. So you need to do this:
empreader = csv.DictReader( empcsvfile, empfieldnames, delimiter='|')
Second, you aren't appending the items to the employee dictionary. You probably should create a key called 'items' on each employee dictionary object and append the items to that list. Like this:
for emprow in empreader:
emprow['items'] = [] # create a list to hold items for this employee
...
for itemrow in itemreader:
...
emprow['items'].append(itemrow) # append an item for this employee
Third, each time you loop through an employee, you need to go back to the top of the item csv file. You have to realize that once python reads to the bottom of a file it won't just go back to the top of it on the next loop. You have to tell it to do that. Right now, your code reads through the item.csv file after the first employee is processed then stays there at the bottom of the file for all the other employees. You have to use seek(0) to tell it to go back to the top of the file for each employee.
for emprow in empreader:
emprow['items'] = []
output.append(emprow)
itemcsvfile.seek(0)
for itemrow in itemreader:
if(itemrow["empid"]==emprow["empid"]):
emprow['items'].append(itemrow)
Columns are not matching:
empid | empname | empemail
empfieldnames = ("empid","name","phone","email")
empid | itemid | itemname | itemqty
itemfieldnames = ("empid","itemid","itemname","itemdesc","itemqty")
We use , usually instead of | in CSV
What's more, you need to replace ' into " in JSON
Related
I am trying to create a system where you can input entries, but the created time & updated time doesn't update.
Here is my code:
from painlessdb import Schema, PainlessDB
from datetime import datetime
import os
schema = {
"entries": {
"tag": Schema.types.int(),
"content": Schema.types.text(),
"created": Schema.types.datetime(),
"updated": Schema.types.datetime(),
},
"amount": Schema.types.int(default=0),
}
db_path = os.path.join(os.getcwd(), 'Others/data.pldb') # .pldb is db file extension
database = PainlessDB(db_path, schema_data=schema)
def add_entry():
amount = database.get("amount").value
current_time = datetime.now()
content = input("Enter the contents of this entry: ")
database.create("entries", fields=database.fields(
tag=amount,
content=content,
created=current_time,
updated=current_time))
database.update("amount", value=amount + 1)
print("Entry created.")
If I run add_entry(), the code runs fine and exits without issue. When I use another function to list the entries:
def list_entries():
query = database.get("entries", multiple=True)
print("No | Content | Created | Updated")
for entry in query:
created_string = entry.created.strftime("%d/%m/%Y - %H:%M:%S")
updated_string = entry.updated.strftime("%d/%m/%Y - %H:%M:%S")
print(f"{entry.tag} | {entry.content} | {created_string} | {updated_string}")
I get this output:
No | Content | Created | Updated
1 | sssss | 21/11/2022 - 22:00:14 | 21/11/2022 - 22:00:14
2 | srsad | 21/11/2022 - 22:00:14 | 21/11/2022 - 22:00:14
3 | kllskk | 21/11/2022 - 22:00:14 | 21/11/2022 - 22:00:14
You can see that the created datetime and updated datetime is somehow stuck to the time I created the first entry (1st entry was created last night, and 3rd entry was just made 5 minutes ago, they are nearly 13 hours apart). I don't think it has anything to do with the database package which I'm using (PainlessDB), but you can take a look here just in case: https://github.com/AidenEllis/PainlessDB/tree/main/docs
I would appreciate any help. Thanks!
Context
I am trying to filter a list of objects based on the value of a reverse foreign key attribute.
I was able to solve it at the view level but, but other attempts to solve using ORM feature result in additional queries.
The outcome I want to is queryset with all objects, but related fkey objects are filtered inside each object.
Sample Models
class Student(models.Model):
name = models.CharField(max_length=128)
class Subject(models.Model):
title = models.CharField(max_length=128)
class Grade(models.Model):
student = models.ForeignKey("Student", related_name="grades", on_delete=models.CASCADE)
subject = models.ForeignKey("Subject", related_name="grades", on_delete=models.CASCADE)
value = models.IntegerField()
Given the Fixtures
+------+------------------------+
| name | subject | grade_value |
+------+----------+-------------+
| beth | math | 100 |
| beth | history | 100 |
| beth | science | 100 |
| mark | math | 90 |
| mark | history | 90 |
| mark | science | 90 |
| mike | math | 90 |
| mike | history | 80 |
| mike | science | 80 |
+------+----------+-------------+
Desired Outcome
I want to render a list of students, but only include math and history grades.
For Example, maybe I want a list of students, but only include a subset of their grades:
GET students/?subjects=math,history
Which are filtered might be provided in the request, or hard coded.
If possible we can leave this outside the scope of this question, and assume the filtering parameters are fixed to math and history.
{
"students": [
{
"name": "beth",
"grades": [
{"subject": "math", "grade": 100 },
{"subject": "history", "grade": 100 },
// Exclude one or more grades - eg.
// science grade not included
]
},
...
]
}
Attempted Solution
Simple Filter
Just filtering. I guess this filters all students which have a grade with subjects in list, which is all.
queryset = Students.objects.all()\
.prefetch_related("grades")\
.filter(grades__subject__in=["math", "history"])
)
# all grades for each student eg.
...
"grades": [
{"subject": "math", "grade": 100 },
{"subject": "history", "grade": 100 },
{"subject": "science", "grade": 100 },
]
...
Subquery
I don't have a great grasp on how subqueries work, but using some examples I had I tried:
subjects = Subject.objects.filter(
name__in=["math", "history"]
)
queryset = Students.objects.all()\
.prefetch_related("grades")\
.filter(grades__subject__name__in=Subquery(subjects.values("name")))
And another variation:
grades = Grades.objects.filter(
student_id=OuterRef("id"), subject__name__in=["math", "history"]
)
queryset = Students.objects.all()\
.prefetch_related("grades")\
.filter(grades__pk__in=Subquery(grades.values("pk)))
Both returned students with all grades.
Workaround Solution
This solutions filters grades using python. It works but I would rather get this working with querysets
# in view:
serializer = StundentSerializer(queryset, many=True)
response_data = serializer.data
for student in response_data:
student.grades = [g for g in students.grades if g["subject"] in ["math", "history"]]
...
# return response_data
You can use the Prefetch object: https://docs.djangoproject.com/en/3.0/ref/models/querysets/#django.db.models.Prefetch
For e.g.:
qs = Students.objects.all().prefetch_related(
Prefetch('grades', queryset=Grade.objects.filter(subject__title__in=["math", "history"])
)
qs[0].grades.all() will now only have math and history grades.
Optionally you can provide the to_attr='math_history_grades' argument to Prefetch, so then you'll access the grades by: qs[0].math_history_grades.all()
I am looking for the ways to change the headings of Cucumber's Data Table to the side. So it will make the feature file readable.
Ordinary way:
| Name | Email | Phone No. | ......... |
| John | i#g.net | 098765644 | ......... |
It can be a very wide data table and I would have to scroll back and forth.
Desired way:
| Name | John |
| Email | i#g.net |
| Phone No. | 098765444 |
.
.
.
There are a small number of examples in Java and Ruby. But I am working with Python.
I had tried many different things like numpy.transpose(), converting them to list. But it won't work because the Data Table's format is:
[<Row['Name','John'],...]
You can implement this behaviour quite simply yourself, here is my version:
def tabledict(table, defaults, aliases = {}):
"""
Converts a behave context.table to a dictionary.
Throws NotImplementedError if the table references an unknown key.
defaults should contain a dictionary with the (surprise) default values.
aliases makes it possible to map alternative names to the keys of the defaults.
All keys of the table will be converted to lowercase, you sould make sure that
you defaults and aliases dictionaries also use lowercase.
Example:
Given the book
| Property | Value |
| Title | The Tragedy of Man |
| Author | Madach, Imre |
| International Standard Book Number | 9631527395 |
defaults = { "title": "Untitled", "author": "Anonymous", "isbn": None, "publisher": None }
aliases = { "International Standard Book Number" : "isbn" }
givenBook = tabledict(context.table, defaults, aliases)
will give you:
givenBook == {
"title": "The Tragedy of Man",
"author": "Madach, Imre",
"isbn": 9631527395,
"publisher": None
}
"""
initParams = defaults.copy()
validKeys = aliases.keys()[:] + defaults.keys()[:]
for row in table:
name, value = row[0].lower(), row[1]
if not name in validKeys:
raise NotImplementedError(u'%s property is not supported.'%name)
if name in aliases: name = aliases[name]
initParams[name] = value
return initParams
This doesn't look like it's related to numpy.
pivoting a list of list is often done with zip(*the_list)
This will return a pivoted behave table
from behave.model import Table
class TurnTable(unittest.TestCase):
"""
"""
def test_transpose(self):
table = Table(
['Name', 'John', 'Mary'],
rows=[
['Email', "john#example.com", "mary#example.com"],
['Phone', "0123456789", "9876543210"],
])
aggregate = [table.headings[:]]
aggregate.extend(table.rows)
pivoted = list(zip(*aggregate))
self.assertListEqual(pivoted,
[('Name', 'Email', 'Phone'),
('John', 'john#example.com', '0123456789'),
('Mary', 'mary#example.com', '9876543210')])
pivoted_table = Table(
pivoted[0],
rows=pivoted[1:])
mary = pivoted_table.rows[1]
self.assertEqual(mary['Name'], 'Mary')
self.assertEqual(mary['Phone'], '9876543210')
you can also have a look at https://pypi.python.org/pypi/pivottable
I’m having a problem printing a table with terminaltables.
Here's my main script:
from ConfigParser import SafeConfigParser
from terminaltables import AsciiTable
parser = SafeConfigParser()
parser.read('my.conf')
for section_name in parser.sections():
description = parser.get(section_name,'description')
url = parser.get(section_name,'url')
table_data = [['Repository', 'Url', 'Description'], [section_name, url, description]]
table = AsciiTable(table_data)
print table.table
and here's the configuration file my.conf:
[bug_tracker]
description = some text here
url = http://localhost.tld:8080/bugs/
username = dhellmann
password = SECRET
[wiki]
description = foo bar bla
url = http://localhost.tld:8080/wiki/
username = dhellmann
password = SECRET
This print me a table for each entry like this:
+-------------+---------------------------------+------------------------+
| Repository | Url | Description |
+-------------+---------------------------------+------------------------+
| bug_tracker | http://localhost.foo:8080/bugs/ | some text here |
+-------------+---------------------------------+------------------------+
+------------+---------------------------------+-------------+
| Repository | Url | Description |
+------------+---------------------------------+-------------+
| wiki | http://localhost.foo:8080/wiki/ | foo bar bla |
+------------+---------------------------------+-------------+
but what I want is this:
+-------------+---------------------------------+------------------------+
| Repository | Url | Description |
+-------------+---------------------------------+------------------------+
| bug_tracker | http://localhost.foo:8080/bugs/ | some text here |
+-------------+---------------------------------+------------------------+
| wiki | http://localhost.foo:8080/wiki/ | foo bar bla |
+-------------+---------------------------------+------------------------+
How can I modify the script to get this output?
The problem is that you recreate table_data and table on each iteration of the loop. You print on each iteration, and then the old data gets thrown away and a new table gets started from scratch. There’s no overlap in the body of the tables you’re creating.
You should have a single table_data, which starts with the headings, then you gather all the table data before printing anything. Add the new entries on each iteration of the loop, and put the print statement after the for loop is finished. Here’s an example:
from ConfigParser import SafeConfigParser
from terminaltables import AsciiTable
parser = SafeConfigParser()
parser.read('my.conf')
table_data = [['Repository', 'Url', 'Description']]
for section_name in parser.sections():
description = parser.get(section_name,'description')
url = parser.get(section_name,'url')
table_data.append([section_name, url, description])
table = AsciiTable(table_data)
print table.table
and here’s what it outputs:
+-------------+---------------------------------+----------------+
| Repository | Url | Description |
+-------------+---------------------------------+----------------+
| bug_tracker | http://localhost.tld:8080/bugs/ | some text here |
| wiki | http://localhost.tld:8080/wiki/ | foo bar bla |
+-------------+---------------------------------+----------------+
If you want to have a horizontal rule between the bug_tracker and wiki lines, then you need to set table.inner_row_border to True. So you’d replace the last two lines with:
table = AsciiTable(table_data)
table.inner_row_border = True
print table.table
I am programming my first website with flask one of my sections is a list of all sub-users for the teacher's class so how can I use prettytable with flask to get a table my end goal is a table that looks like this (just the data)
Teacher | Student | Id | GPA | Last sign learned
----------------------------------------------------------------
Medina | John doe | 19500688 | 4.0 | Bad
Medina | Samantha babcock | 91234094 | 2.5 | Toilet
Jonson | Steven smith | 64721881 | 3.0 | Santa
How can I do this preferably with Pretty table but any method would be great!
Hello so your form is simple to create but so lets begin with static information with python
def function():
from prettytable import *
table = PrettyTable(["Teacher","Student"," ID","GPA"," Last sign learned "])
table.add_row(["Medina","John doe","19500688","4.0","Bad"])
table.add_row(["Medina","Samantha babcock ","91234094","2.5","Toilet"])
table.add_row(["Jonson","Steven smith","64721881","3.0","Santa"])
return render_template("info.html", tbl=table.get_html_string(attributes = {"class": "foo"}))
Now for you HTML:
{%extends "template.html"%}
{{tbl|safe}}