Python Forum
Database management in csv format - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Database management in csv format (/thread-19947.html)



Database management in csv format - shahmirv - Jul-21-2019

Hi,
I have a software which exports itemwise daily information in a csv format.
I wish to create separate csv files, making the item name as the file name and each such file shall contain the respective items daily database.
Also, the code shall be able to append the new data in the respective csv file on a daily basis with a click.
I am new to programming and thereby would really appreciate a model code provided by the community so that I can learn based on the same.

Thanking in anticipation.

The csv exported daily from the software contains csv in following format
Item, Date, Price, Quantity, Value1, Value2, Value3, Value4 [These are the headers]
A, 21-07-2018, 100, 1000, 5, 8, 9, 4
B, 21-07-2018, 200, 7000, 2, 6, 7, 9
C, 21-07-2018, 400, 8000, 5, 8, 2, 3
D, 21-07-2018, 300, 9000, 3, 3, 9, 4
E, 21-07-2018, 500, 6000, 2, 6, 4, 7

The above is extracted for each day.

The desired output will be

Filename - A.csv containing the following values
Item, Date, Price, Quantity, Value1, Value2, Value3, Value4 [These are the headers]
A, 21-07-2018, 100, 1000, 5, 8, 9, 4
A, 22-07-2018, 200, 5000, 6, 4, 8, 3
A, 23-07-2018, 300, 7000, 7, 9, 3, 5
A, 24-07-2018, 700, 9000, 3, 5, 2, 4
A, 25-07-2018, 800, 4000, 2, 6, 6, 6

[On a similar fashion, files B.csv, C.csv, D.csv and E.csv is also required to be generated]


RE: Database management in csv format - Larz60+ - Jul-21-2019

This looks to be a very simple application.
What have you tried so far?
I would study: https://docs.python.org/3/library/csv.html
if converting from csv to sql, perhaps pandas, something like: https://www.learndatasci.com/tutorials/python-pandas-tutorial-complete-introduction-for-beginners/
many more tutorials available, as well as YouTube videos


RE: Database management in csv format - scidam - Jul-21-2019

Take a look at Pandas. Using Pandas your pseudo-code would be as follows:

import pandas as pd
data = pd.read_csv('your_source_file_name')

for g, d in data.groupby('Item').groups.items():
    data.loc[d].to_csv("{}".format(str(g)))
I didn't test the code above, probably you will need to slightly modify it.


RE: Database management in csv format - shahmirv - Jul-11-2020

(Jul-21-2019, 11:33 PM)scidam Wrote: Take a look at Pandas. Using Pandas your pseudo-code would be as follows:

import pandas as pd
data = pd.read_csv('your_source_file_name')

for g, d in data.groupby('Item').groups.items():
    data.loc[d].to_csv("{}".format(str(g)))
I didn't test the code above, probably you will need to slightly modify it.

It actually just replaces the old data with new one... I wish to add the new data below the previous data.