skip to Main Content

I want to convert all the rows into array.
E.g. I have multiple values which is stored in CSV.
Suppose in A1 position value is Message
In B1 position value is Field=1234
In C1 position value is Field=0023

Like from A1 to around AZ1 I have data in rows.

So I want to convert it into array after that stored in postgres by different data fields.

In my csv there is no any column name.

I’m expecting the python code for this.
After that I want to catch only particular data fields value to created column names.


This is my data which is stored like this in csv. So, I want to segregate this data in database.

My CSV Data:- My data

I want to segregate like this in database from csv file:- Segregate like this in & store in database

2

Answers


  1. import csv 
    with open("file.csv") as file:
        # generator 
        reader = csv.reader(file)
        for row in reader:
             # this will print each row as a list/array
             print(row)
    
    Login or Signup to reply.
  2. You can use pandas for this. For Example

    # importing the module
    import pandas as pd
      
    # creating a DataFrame
    data = {'Name' : ['Sana', 'Manaan', 'Rizwan', 
                     'Uman', 'Usama'],  
            'Computer' : [8, 5, 6, 9, 7],  
            'Farsi' : [7, 9, 5, 4, 7], 
            'Urdu' : [7, 4, 7, 6, 8]} 
    df = pd.DataFrame(data)
    print("Original DataFrame")
    display(df)
      
    print("Value of row 3 (Uman)")
    display(df.iloc[3]) # Getting the row using iloc
    
    array = []
    
    for i in df.iloc[3]:
      array.append(i)
    
    display(array)
    

    first you can read the data from csv. In the above example I just created a simple DataFrame. After that use iloc method to select the specific row. and then just append it in the array. Remember that index starts from 0 so 3 here indicates the 4th row.
    Output:

    Output

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search