Home > Software engineering >  Reading first element of each column and then the entire row in csv file
Reading first element of each column and then the entire row in csv file

Time:10-26

I have a csv file, like this:

Account     Email       User_Id     User Type   Base Role   Last Login
123456  [email protected]    111111         inter      user         7
7891011 [email protected]    222222         inter      user         6
121314  [email protected]    333333         inter      user         5

and there will be 50 other rows like this. Each account can have multiple users. and also the same account can be listed on the file multiple times. I have to create a new csv file for each account. For each account, I have to select the entire row and copy its contents. How do I go about this? How do I select:

for each account number
   if a csv file for this account does not exist already
       create a new file
   copy the entire now and paste it in the new csv file

I can create a new csv file with this:

with open("test.csv") as fp

but I am stuck with how do I go about selecting each account number and then copying and pasting the contents of that row in a new file. I am new to Python. Please help

CodePudding user response:

Python comes with a csv module by default

import csv

def get_firsts(csvfile, skip_first=True):
    with open(csvfile, 'r') as f:
        data = csv.reader(f, delimiter=',')
        if skip_first:
            _ = next(data)
        firsts = [row[0] for row in data]
    return firsts

This returns a list with only the first element of each row, you can get rid of the first if it's a column name.

CodePudding user response:

you can use pandas in python.

import pandas as pd

if u have DataFrame -> its ok

if u don't u can use this line to convert your CSV to the DataFrame

df = pd.read_csv('your_csv_file.csv')

now you can select your data using DataFrame functions like this.

new_df = df.loc[df['Account'] == 123456]

new_df is a DataFrame too. you can save your result DataFrame using :

new_df.to_csv('results.csv')
  • Related