简体   繁体   中英

How to read required values from a list and store it in csv using python?

I have a response in the list format. I have to read the list and store only required values in CSV file in ';'separated values.

 row = "[('Employee', 'Shift', 'IT', '5:15AM', 'Server', 'Manager', 'empemail@cmp.com'),('Employee', 'Shift', 'IT', '6:15AM', 'Server', 'Manager', 'empemail@cmp.com'),('Employee', 'Shift', 'CALL CENTER', '7:15AM', 'Server', 'Manager', 'empemail@cmp.com'),('Employee', 'Shift', 'CALL CENTER', '8:15AM', 'Server', 'Manager', 'empemail@cmp.com')]"

Now I need to store the list in CSV file in the below expected format.

param_Name,Param_Value
p_Name,Employee
p_operationType,IT;IT;CALL CENTER;CALL CENTER
p_Time,5:15 AM;6:15 AM;7:15 AM;8:15 AM
p_avaiablity,Daily
p_email,empemail@cmp.com

I am trying in pandas.

 import csv
 import pandas as pd
 df = pd.DataFrame(data=row).T
 df.to_csv('csvfile.csv',sep=';',index=False,header=False)
 print(df)
 for item in df:
    print(item) 
    #Stuck on how to proceed further...

With this code I am geting a csv file as

param_Name,Param_Value
p_name,Employee;Employee;Employee;Employee
p_avaiablity,Daily;Daily;Daily;Daily

How can I iterate into the list and get only required values and store as expected format?

Hope this helps

import pandas as pd

row = [('Employee', 'Shift', 'IT', '5:15AM', 'Server', 'Manager', 'empemail@cmp.com'),('Employee', 'Shift', 'IT', '6:15AM', 'Server', 'Manager', 'empemail@cmp.com'),('Employee', 'Shift', 'CALL CENTER', '7:15AM', 'Server', 'Manager', 'empemail@cmp.com'),('Employee', 'Shift', 'CALL CENTER', '8:15AM', 'Server', 'Manager', 'empemail@cmp.com')]

# use dataframe constructor properly and name your columns
cols = ['Employee', 'Dont_know_what_this_is', 'department', 'time', 'location', 'status', 'email']
df = pd.DataFrame.from_records(row, columns=cols)

for index, row in df.iterrows():
    # do what you want with your data
    print(row)

# or use 'loc' function to get specific columns
df = df.loc[df.department == 'IT']
print(df)
# then export your csv
df.to_csv('csvfile.csv',sep=';',index=False,header=False)

Outputs:

    Employee Dont_know_what_this_is   department    time location   status             email
0  Employee                  Shift           IT  5:15AM   Server  Manager  empemail@cmp.com
1  Employee                  Shift           IT  6:15AM   Server  Manager  empemail@cmp.com
2  Employee                  Shift  CALL CENTER  7:15AM   Server  Manager  empemail@cmp.com
3  Employee                  Shift  CALL CENTER  8:15AM   Server  Manager  empemail@cmp.com

After loc:

Employee Dont_know_what_this_is department    time location   status             email
0  Employee                  Shift         IT  5:15AM   Server  Manager  empemail@cmp.com
1  Employee                  Shift         IT  6:15AM   Server  Manager  empemail@cmp.com

Is this the output you are looking for? pd.DataFrame(data=row) will give you this. Then you can rename the columns

0   1   2   3   4   5   6
0   Employee    Shift   IT  5:15AM  Server  Manager empemail@cmp.com
1   Employee    Shift   IT  6:15AM  Server  Manager empemail@cmp.com
2   Employee    Shift   CALL CENTER 7:15AM  Server  Manager empemail@cmp.com
3   Employee    Shift   CALL CENTER 8:15AM  Server  Manager empemail@cmp.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM