0

I have a csv file which has contents like the following:

stores.csv

Site, Score, Rank
roolee.com,100,125225
piperandscoot.com,29.3,222166
calledtosurf.com,23.8,361542
cladandcloth.com,17.9,208670
neeseesdresses.com,9.6,251016
...

Here's my model.

models.py

class SimilarStore(models.Model):
    store = models.ForeignKey(Store)
    csv = FileField()

    domain = models.CharField(max_length=100, blank=True)
    score = models.IntegerField(blank=True)
    rank = models.IntegerField(blank=True)

So, I wanna upload the stores.csv file into csv field so that each column data goes into each domain, score, and rank.

I found out some resources making python file to parse data and run it through a command like `python parse.py'. However, I need it to be done by uploading the csv file in Django admin. Can anyone explain how I can do that?

2
  • is the code above your own? what have you tried? Commented Jun 13, 2018 at 23:03
  • There is not any way for parsing csv file. You can read csv file and then set to your fields. you can use pandas For parsing csv file. Commented Jun 14, 2018 at 0:59

2 Answers 2

2

You can read the csv file through pandas and then convert it to your desired object ny using pandas funtion 'infer_objects()'. I hope the code below helps you in this regard,

import pandas as pd
SimilarStore_df = pd.read_csv('./store.csv') #importing csv file as pandas DataFrame
SimilarStore_df.columns = ['Domain', 'Score','Rank']

SimilarStore=SimilarStore_df.infer_objects() #converting DataFrame to object

print(SimilarStore)
print(SimilarStore.columns)

Output:

               Domain  Score    Rank
0          roolee.com  100.0  125225
1   piperandscoot.com   29.3  222166
2    calledtosurf.com   23.8  361542
3    cladandcloth.com   17.9  208670
4  neeseesdresses.com    9.6  251016
Index(['Domain', 'Score', 'Rank'], dtype='object')
Sign up to request clarification or add additional context in comments.

Comments

0

Just find the table name in your Database also find the corresponding column names with the table and use the to_sql command in pandas

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.