Search code examples
pythondjango-modelsimportdjango-views

Split a CSV file with different section and column headers in django models


Please i'm a newbie in python, I have a CSV file with this type of structure. How do i split this csv after uploading and create or update my django database with the data for model.

About
no,name,shape,type
1,John,circle,metal
..................
..................

Address
no,street,postcode,city,County
1,peel,sd1 q23,london,east london
...............................

about 9 sections, a major section header which could be ignored while parsing into the database, the first row are headers, and data for each column

I was able to create a model and import data from a single row of headers into my model. I've tried using row.get('title') to split the csv file but it's not working. I can't find any example directly related to this yet. thanks


Solution

  • So I was able to split the csv files and parse into my django models by doing this:

    def import_and_process_csv(request):
        if request.method == 'POST':
            
            data_set = ()
            new_dataset = request.FILES['csv_file']
            
            
            if not new_dataset.name.endswith('csv'):
                messages.info(request,'wrong format')
                return render(request,'import.html')
            else:
                messages.info(request, 'File uploaded successfully')
    
           
            csv_content = new_dataset.read().decode('utf-8')
            
    
            # Use io.StringIO to create a file-like object from the string content
            csv_string_io = io.StringIO(csv_content)
            next(csv_string_io)
            
            data_set = csv.reader(csv_string_io, delimiter=',')
            
    
            #Read the data for rows in store and parse into models.
            specific_values = ['biscuit']
            printed_rows_indices = set()
    
        # Iterate over each specific value
            for desired_value in specific_values:
            # Initialize a flag to track if the desired row value is found
                found_row_value = False
    
            # Initialize a list to store rows before the desired value
                rows_before_desired_value1 = []
    
                for index, row in enumerate(data_set):
                # Check if the desired value is found in the current row
                    if desired_value in row:
                        found_row_value = True
                        break
                    # Stop the CSV reader when the desired value is found
                             
                    rows_before_desired_value1.append(row)  #rows before Biscuit
                next
            
                    
                for i,column in enumerate(rows_before_desired_value1):
                    
                    if i !=0:                   
                        if column and not all(element == ',' for element in column):  # Check if the list is not empty and not comma-only
                                            
                            if len(column) > 0:                     
                                myModel.objects.update_or_create(
                                My_Object1 =  column[0],
                                My_Object2 = column[1],
                                
                        )
                                    
        return render(request,'import.html')