Python/Numpy(CSV): Finding values, appending another csv










2















I have found other posts very closely related to this, but they are not helping.



I have a Master CSV file, and I need to find specific 'string' from the third column. Shown below:



Name,ID,Title,Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
Joshua Morales,MF6B9X,Tech_Rep, 08-Nov-2016,948,740,8,8
Betty García,ERTW77,SME, 08-Nov-2016,965,854,15,12
Kathleen Marrero,KTD684,Probation, 08-Nov-2016,946,948,na,na
Mark León,GSL89D,Tech_Rep, 08-Nov-2016,951,844,6,4


The ID column is unique, and so I was trying to find 'KTD684'(for expample). Once found, I need to export the values of "Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned" and "Prj2_solved".



The export would be to a file 'KTD684.csv'(same as ID) where there is already headers available 'Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved'



So far (as I am a non-programmer) I have not been able to draft this, but can one please be kind to guide me in:



  1. Finding the row with the element 'KTD684'.

  2. Selecting the values of the below from that row:
    ['Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved']

  3. Appending the file with the ID name itself please('KTD684.csv')

I need to perform this for 45 userIDs, and now with hiring in company, its 195. I tried to write excel macro(didn't work either), but I feel python is most reliable.



I know I need to at least show the basic progress, but after over 2 months of trying to learn from someone, I'm still unable to find the element in this csv.










share|improve this question
























  • So for ID = KTD684 you want to write "Mark León,GSL89D,Tech_Rep, 08-Nov-2016,951,844,6,4" into a file called "KTD684.csv", is this correct?

    – Jonathan R
    Nov 14 '18 at 14:23











  • dear Jonathan, yes please, I would need the data pertaining to the specific ID: 08-Nov-2016,951,844,6,4

    – Lokkii9
    Nov 14 '18 at 16:55















2















I have found other posts very closely related to this, but they are not helping.



I have a Master CSV file, and I need to find specific 'string' from the third column. Shown below:



Name,ID,Title,Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
Joshua Morales,MF6B9X,Tech_Rep, 08-Nov-2016,948,740,8,8
Betty García,ERTW77,SME, 08-Nov-2016,965,854,15,12
Kathleen Marrero,KTD684,Probation, 08-Nov-2016,946,948,na,na
Mark León,GSL89D,Tech_Rep, 08-Nov-2016,951,844,6,4


The ID column is unique, and so I was trying to find 'KTD684'(for expample). Once found, I need to export the values of "Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned" and "Prj2_solved".



The export would be to a file 'KTD684.csv'(same as ID) where there is already headers available 'Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved'



So far (as I am a non-programmer) I have not been able to draft this, but can one please be kind to guide me in:



  1. Finding the row with the element 'KTD684'.

  2. Selecting the values of the below from that row:
    ['Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved']

  3. Appending the file with the ID name itself please('KTD684.csv')

I need to perform this for 45 userIDs, and now with hiring in company, its 195. I tried to write excel macro(didn't work either), but I feel python is most reliable.



I know I need to at least show the basic progress, but after over 2 months of trying to learn from someone, I'm still unable to find the element in this csv.










share|improve this question
























  • So for ID = KTD684 you want to write "Mark León,GSL89D,Tech_Rep, 08-Nov-2016,951,844,6,4" into a file called "KTD684.csv", is this correct?

    – Jonathan R
    Nov 14 '18 at 14:23











  • dear Jonathan, yes please, I would need the data pertaining to the specific ID: 08-Nov-2016,951,844,6,4

    – Lokkii9
    Nov 14 '18 at 16:55













2












2








2








I have found other posts very closely related to this, but they are not helping.



I have a Master CSV file, and I need to find specific 'string' from the third column. Shown below:



Name,ID,Title,Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
Joshua Morales,MF6B9X,Tech_Rep, 08-Nov-2016,948,740,8,8
Betty García,ERTW77,SME, 08-Nov-2016,965,854,15,12
Kathleen Marrero,KTD684,Probation, 08-Nov-2016,946,948,na,na
Mark León,GSL89D,Tech_Rep, 08-Nov-2016,951,844,6,4


The ID column is unique, and so I was trying to find 'KTD684'(for expample). Once found, I need to export the values of "Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned" and "Prj2_solved".



The export would be to a file 'KTD684.csv'(same as ID) where there is already headers available 'Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved'



So far (as I am a non-programmer) I have not been able to draft this, but can one please be kind to guide me in:



  1. Finding the row with the element 'KTD684'.

  2. Selecting the values of the below from that row:
    ['Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved']

  3. Appending the file with the ID name itself please('KTD684.csv')

I need to perform this for 45 userIDs, and now with hiring in company, its 195. I tried to write excel macro(didn't work either), but I feel python is most reliable.



I know I need to at least show the basic progress, but after over 2 months of trying to learn from someone, I'm still unable to find the element in this csv.










share|improve this question
















I have found other posts very closely related to this, but they are not helping.



I have a Master CSV file, and I need to find specific 'string' from the third column. Shown below:



Name,ID,Title,Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
Joshua Morales,MF6B9X,Tech_Rep, 08-Nov-2016,948,740,8,8
Betty García,ERTW77,SME, 08-Nov-2016,965,854,15,12
Kathleen Marrero,KTD684,Probation, 08-Nov-2016,946,948,na,na
Mark León,GSL89D,Tech_Rep, 08-Nov-2016,951,844,6,4


The ID column is unique, and so I was trying to find 'KTD684'(for expample). Once found, I need to export the values of "Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned" and "Prj2_solved".



The export would be to a file 'KTD684.csv'(same as ID) where there is already headers available 'Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved'



So far (as I am a non-programmer) I have not been able to draft this, but can one please be kind to guide me in:



  1. Finding the row with the element 'KTD684'.

  2. Selecting the values of the below from that row:
    ['Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved']

  3. Appending the file with the ID name itself please('KTD684.csv')

I need to perform this for 45 userIDs, and now with hiring in company, its 195. I tried to write excel macro(didn't work either), but I feel python is most reliable.



I know I need to at least show the basic progress, but after over 2 months of trying to learn from someone, I'm still unable to find the element in this csv.







python csv numpy find append






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 14:53









James Z

11.2k71935




11.2k71935










asked Nov 14 '18 at 14:11









Lokkii9Lokkii9

207




207












  • So for ID = KTD684 you want to write "Mark León,GSL89D,Tech_Rep, 08-Nov-2016,951,844,6,4" into a file called "KTD684.csv", is this correct?

    – Jonathan R
    Nov 14 '18 at 14:23











  • dear Jonathan, yes please, I would need the data pertaining to the specific ID: 08-Nov-2016,951,844,6,4

    – Lokkii9
    Nov 14 '18 at 16:55

















  • So for ID = KTD684 you want to write "Mark León,GSL89D,Tech_Rep, 08-Nov-2016,951,844,6,4" into a file called "KTD684.csv", is this correct?

    – Jonathan R
    Nov 14 '18 at 14:23











  • dear Jonathan, yes please, I would need the data pertaining to the specific ID: 08-Nov-2016,951,844,6,4

    – Lokkii9
    Nov 14 '18 at 16:55
















So for ID = KTD684 you want to write "Mark León,GSL89D,Tech_Rep, 08-Nov-2016,951,844,6,4" into a file called "KTD684.csv", is this correct?

– Jonathan R
Nov 14 '18 at 14:23





So for ID = KTD684 you want to write "Mark León,GSL89D,Tech_Rep, 08-Nov-2016,951,844,6,4" into a file called "KTD684.csv", is this correct?

– Jonathan R
Nov 14 '18 at 14:23













dear Jonathan, yes please, I would need the data pertaining to the specific ID: 08-Nov-2016,951,844,6,4

– Lokkii9
Nov 14 '18 at 16:55





dear Jonathan, yes please, I would need the data pertaining to the specific ID: 08-Nov-2016,951,844,6,4

– Lokkii9
Nov 14 '18 at 16:55












3 Answers
3






active

oldest

votes


















0














This is an ideal use-case for pandas:



import pandas as pd

id_list = ['KTD684']

df = pd.read_csv('input.csv')
# Only keep values that are in 'id_list'
df = df[df['ID'].isin(id_list)]

gb = df.groupby('ID')
for name, group in gb:
with open('.csv'.format(name), 'a') as f:
group.to_csv(f, header=False, index=False,
columns=["Date", "Prj1_Assigned", "Prj1_closed",
"Prj2_assigned", "Prj2_solved"])


This will open the CSV, only select rows that are in your list (id_list), group by the values in the ID column and save individual CSV files for each unique ID. You just need to expand id_list to have the ids you are interested in.




Extended example:



Reading in the CSV results in a DataFrame object like this:



df = pd.read_csv('input.csv')
Name ID Title Date Prj1_Assigned
0 Joshua Morales MF6B9X Tech_Rep 08-Nov-2016 948
1 Betty García ERTW77 SME 08-Nov-2016 965
2 Kathleen Marrero KTD684 Probation 08-Nov-2016 946
3 Mark León GSL89D Tech_Rep 08-Nov-2016 951

Prj1_closed Prj2_assigned Prj2_solved
0 740 8 8
1 854 15 12
2 948 na na
3 844 6 4


If you just select KTD684 and GSL89D:



id_list = ['KTD684', 'GSL89D']
df = df[df['ID'].isin(id_list)]
Name ID Title Date Prj1_Assigned
2 Kathleen Marrero KTD684 Probation 08-Nov-2016 946
3 Mark León GSL89D Tech_Rep 08-Nov-2016 951

Prj1_closed Prj2_assigned Prj2_solved
2 948 na na
3 844 6 4


The groupby operation groups on ID and export each unique ID to a CSV file resulting in:



KTD684.csv
Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,946,948,na,na

GSL89D.csv
Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,951,844,6,4





share|improve this answer

























  • Dear Alex, thanks so much for offering your take on this.. I find the following error please File "<ipython-input-5-e4be53771aeb>", line 4 header=["Date", "Prj1_Assigned", "Prj1_closed", ^ SyntaxError: keyword argument repeated [This could be something minor, or maybe even my mistake, but I've been hustling with this since a while]

    – Lokkii9
    Nov 14 '18 at 17:39











  • I’ve updated the answer, I accidentally duplicated a parameter.

    – Alex
    Nov 14 '18 at 18:15











  • Thankss a tonn Alex...It really worked well... :) :) :)

    – Lokkii9
    Nov 14 '18 at 19:30











  • I'm glad it worked, take a look at the pandas docs to get an idea of what you can do with the library. If you get stuck you can ask at the pandas tag.

    – Alex
    Nov 14 '18 at 22:11


















1














If I understand your problem correctly; You need to read from 2 input files:



  • 1 containing the users IDs you are looking for


  • 2 containing the project data related to users


In that fashion something like this would find all the users you specify in 1 in file 2 and write them out to result.csv




Sepicify your search IDs in search_for.csv. Keep in mind that this
will revrite your result.csv every time you run it.




import csv
import sys
import os


inputPatterns = open(os.curdir + '/search_for.csv', 'rt')

# Reader for the IDs (users) you are looking to find (key)
reader = csv.reader(inputPatterns)

ids =

# reading the IDs you are looking for from search_for.csv
for row in reader:
ids.append(row[0])
inputPatterns.close()

# Let's see if any of the user IDs we are looking for has any project related info
# if so write them to your output CSV
for userID in ids:
# Organization list with names and Company ID and reader
userList = open(os.curdir + '/users.csv', 'rt')
reader = csv.reader(userList)

# This will be the output file
result_f = open(os.curdir + "/" + userID + ".csv", 'w')
w = csv.writer(result_f)
# Writing header information
w.writerow(['Date', 'Prj1_Assigned', 'Prj1_closed', 'Prj2_assigned', 'Prj2_solved'])

# Scanning for projects for user and appending them
for row in reader:
if userID == row[1]:
w.writerow([row[3], row[4], row[5], row[6], row[7]])
result_f.close()
userList.close()


For example, search_for.csv looks like this



if your search_for.csv looks like this






share|improve this answer

























  • Dear Gabor, thanks so very much for providing your solution on this. But when I execute the code, this gives me output files with the usernames as you had mentioned. Actually hoped it would be ID names(.csv), moreover, the output csv files, are only having the headers in them, but there are no data pertaining to them MF6B9X,Tech_Rep, 08-Nov-2016,948,740,8,8 ... kindly help me with the steps of: [1]Opening Master file(Trials.csv) - [2]Finding the exact column where 'KTD684' is available - [3]Appending the(already existing KTD684.csv with the 5 values from Date... in the next available row pls

    – Lokkii9
    Nov 14 '18 at 17:55











  • Hi Lokkii, Yes I misunderstood your question in the first version. This version was uploaded before your comment and should do exactly what you are looking for. I'm also new to StackOverflow so I didn't know that I need to notify you on update :) But just to clarify. If you use input files: search_for.csv and users.csv (instead of Trials.csv) in the same directory as this python file. It should work as desired; Creates useID.csv files. I see you already accepted an answer so the problem is solved. One note: This problem can be solved in excel pivot table very quickly.

    – Gábor Hertelendy
    Nov 15 '18 at 3:28












  • Hi Gabor :) thanks soo very much for this..It does work perfectly well... :) Appreciate your time!

    – Lokkii9
    Nov 15 '18 at 16:24


















0














Here's a pure python approach which reads the master .csv file with csv.DictReader, matches the ids, and appends the file data into a new or existing .csv file with csv.DictWriter():



from csv import DictReader
from csv import DictWriter

from os.path import isfile

def export_csv(user_id, master_csv, fieldnames, key_id, extension=".csv"):
filename = user_id + extension
file_exists = isfile(filename)

with open(file=master_csv) as in_file, open(
file=filename, mode="a", newline=""
) as out_file:

# Create reading and writing objects
csv_reader = DictReader(in_file)
csv_writer = DictWriter(out_file, fieldnames=fieldnames)

# Only write header once
if not file_exists:
csv_writer.writeheader()

# Go through lines and match ids
for line in csv_reader:
if line[key_id] == user_id:

# Modify line and append to file
line = k: v.strip() for k, v in line.items() if k in fieldnames
csv_writer.writerow(line)


Which can be called like this:



export_csv(
user_id="KTD684",
master_csv="master.csv",
fieldnames=["Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned", "Prj2_solved"],
key_id="ID",
)


And produces the following KTD684.csv:



Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,946,948,na,na





share|improve this answer

























  • Dear RoadRunner, thanks really for taking time on this one..kindly find this error which seems to be coming when I execute this. **TypeError Traceback (most recent call last) <ipython-input-7-3ae7e09af9ad> in <module>() 3 master_csv="master.csv", 4 columns=["Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned", "Prj2_solved"], ----> 5 key_id="ID", 6 ) TypeError: export_csv() got an unexpected keyword argument 'columns' ** Is there any way around for this please..

    – Lokkii9
    Nov 14 '18 at 18:05











  • @Lokkii9 No problem. I updated the answer.

    – RoadRunner
    Nov 15 '18 at 1:13











  • Very Grateful for this RoadRunner...sorry for the late response... :) :) :)

    – Lokkii9
    Nov 17 '18 at 15:42










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302202%2fpython-numpycsv-finding-values-appending-another-csv%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














This is an ideal use-case for pandas:



import pandas as pd

id_list = ['KTD684']

df = pd.read_csv('input.csv')
# Only keep values that are in 'id_list'
df = df[df['ID'].isin(id_list)]

gb = df.groupby('ID')
for name, group in gb:
with open('.csv'.format(name), 'a') as f:
group.to_csv(f, header=False, index=False,
columns=["Date", "Prj1_Assigned", "Prj1_closed",
"Prj2_assigned", "Prj2_solved"])


This will open the CSV, only select rows that are in your list (id_list), group by the values in the ID column and save individual CSV files for each unique ID. You just need to expand id_list to have the ids you are interested in.




Extended example:



Reading in the CSV results in a DataFrame object like this:



df = pd.read_csv('input.csv')
Name ID Title Date Prj1_Assigned
0 Joshua Morales MF6B9X Tech_Rep 08-Nov-2016 948
1 Betty García ERTW77 SME 08-Nov-2016 965
2 Kathleen Marrero KTD684 Probation 08-Nov-2016 946
3 Mark León GSL89D Tech_Rep 08-Nov-2016 951

Prj1_closed Prj2_assigned Prj2_solved
0 740 8 8
1 854 15 12
2 948 na na
3 844 6 4


If you just select KTD684 and GSL89D:



id_list = ['KTD684', 'GSL89D']
df = df[df['ID'].isin(id_list)]
Name ID Title Date Prj1_Assigned
2 Kathleen Marrero KTD684 Probation 08-Nov-2016 946
3 Mark León GSL89D Tech_Rep 08-Nov-2016 951

Prj1_closed Prj2_assigned Prj2_solved
2 948 na na
3 844 6 4


The groupby operation groups on ID and export each unique ID to a CSV file resulting in:



KTD684.csv
Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,946,948,na,na

GSL89D.csv
Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,951,844,6,4





share|improve this answer

























  • Dear Alex, thanks so much for offering your take on this.. I find the following error please File "<ipython-input-5-e4be53771aeb>", line 4 header=["Date", "Prj1_Assigned", "Prj1_closed", ^ SyntaxError: keyword argument repeated [This could be something minor, or maybe even my mistake, but I've been hustling with this since a while]

    – Lokkii9
    Nov 14 '18 at 17:39











  • I’ve updated the answer, I accidentally duplicated a parameter.

    – Alex
    Nov 14 '18 at 18:15











  • Thankss a tonn Alex...It really worked well... :) :) :)

    – Lokkii9
    Nov 14 '18 at 19:30











  • I'm glad it worked, take a look at the pandas docs to get an idea of what you can do with the library. If you get stuck you can ask at the pandas tag.

    – Alex
    Nov 14 '18 at 22:11















0














This is an ideal use-case for pandas:



import pandas as pd

id_list = ['KTD684']

df = pd.read_csv('input.csv')
# Only keep values that are in 'id_list'
df = df[df['ID'].isin(id_list)]

gb = df.groupby('ID')
for name, group in gb:
with open('.csv'.format(name), 'a') as f:
group.to_csv(f, header=False, index=False,
columns=["Date", "Prj1_Assigned", "Prj1_closed",
"Prj2_assigned", "Prj2_solved"])


This will open the CSV, only select rows that are in your list (id_list), group by the values in the ID column and save individual CSV files for each unique ID. You just need to expand id_list to have the ids you are interested in.




Extended example:



Reading in the CSV results in a DataFrame object like this:



df = pd.read_csv('input.csv')
Name ID Title Date Prj1_Assigned
0 Joshua Morales MF6B9X Tech_Rep 08-Nov-2016 948
1 Betty García ERTW77 SME 08-Nov-2016 965
2 Kathleen Marrero KTD684 Probation 08-Nov-2016 946
3 Mark León GSL89D Tech_Rep 08-Nov-2016 951

Prj1_closed Prj2_assigned Prj2_solved
0 740 8 8
1 854 15 12
2 948 na na
3 844 6 4


If you just select KTD684 and GSL89D:



id_list = ['KTD684', 'GSL89D']
df = df[df['ID'].isin(id_list)]
Name ID Title Date Prj1_Assigned
2 Kathleen Marrero KTD684 Probation 08-Nov-2016 946
3 Mark León GSL89D Tech_Rep 08-Nov-2016 951

Prj1_closed Prj2_assigned Prj2_solved
2 948 na na
3 844 6 4


The groupby operation groups on ID and export each unique ID to a CSV file resulting in:



KTD684.csv
Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,946,948,na,na

GSL89D.csv
Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,951,844,6,4





share|improve this answer

























  • Dear Alex, thanks so much for offering your take on this.. I find the following error please File "<ipython-input-5-e4be53771aeb>", line 4 header=["Date", "Prj1_Assigned", "Prj1_closed", ^ SyntaxError: keyword argument repeated [This could be something minor, or maybe even my mistake, but I've been hustling with this since a while]

    – Lokkii9
    Nov 14 '18 at 17:39











  • I’ve updated the answer, I accidentally duplicated a parameter.

    – Alex
    Nov 14 '18 at 18:15











  • Thankss a tonn Alex...It really worked well... :) :) :)

    – Lokkii9
    Nov 14 '18 at 19:30











  • I'm glad it worked, take a look at the pandas docs to get an idea of what you can do with the library. If you get stuck you can ask at the pandas tag.

    – Alex
    Nov 14 '18 at 22:11













0












0








0







This is an ideal use-case for pandas:



import pandas as pd

id_list = ['KTD684']

df = pd.read_csv('input.csv')
# Only keep values that are in 'id_list'
df = df[df['ID'].isin(id_list)]

gb = df.groupby('ID')
for name, group in gb:
with open('.csv'.format(name), 'a') as f:
group.to_csv(f, header=False, index=False,
columns=["Date", "Prj1_Assigned", "Prj1_closed",
"Prj2_assigned", "Prj2_solved"])


This will open the CSV, only select rows that are in your list (id_list), group by the values in the ID column and save individual CSV files for each unique ID. You just need to expand id_list to have the ids you are interested in.




Extended example:



Reading in the CSV results in a DataFrame object like this:



df = pd.read_csv('input.csv')
Name ID Title Date Prj1_Assigned
0 Joshua Morales MF6B9X Tech_Rep 08-Nov-2016 948
1 Betty García ERTW77 SME 08-Nov-2016 965
2 Kathleen Marrero KTD684 Probation 08-Nov-2016 946
3 Mark León GSL89D Tech_Rep 08-Nov-2016 951

Prj1_closed Prj2_assigned Prj2_solved
0 740 8 8
1 854 15 12
2 948 na na
3 844 6 4


If you just select KTD684 and GSL89D:



id_list = ['KTD684', 'GSL89D']
df = df[df['ID'].isin(id_list)]
Name ID Title Date Prj1_Assigned
2 Kathleen Marrero KTD684 Probation 08-Nov-2016 946
3 Mark León GSL89D Tech_Rep 08-Nov-2016 951

Prj1_closed Prj2_assigned Prj2_solved
2 948 na na
3 844 6 4


The groupby operation groups on ID and export each unique ID to a CSV file resulting in:



KTD684.csv
Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,946,948,na,na

GSL89D.csv
Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,951,844,6,4





share|improve this answer















This is an ideal use-case for pandas:



import pandas as pd

id_list = ['KTD684']

df = pd.read_csv('input.csv')
# Only keep values that are in 'id_list'
df = df[df['ID'].isin(id_list)]

gb = df.groupby('ID')
for name, group in gb:
with open('.csv'.format(name), 'a') as f:
group.to_csv(f, header=False, index=False,
columns=["Date", "Prj1_Assigned", "Prj1_closed",
"Prj2_assigned", "Prj2_solved"])


This will open the CSV, only select rows that are in your list (id_list), group by the values in the ID column and save individual CSV files for each unique ID. You just need to expand id_list to have the ids you are interested in.




Extended example:



Reading in the CSV results in a DataFrame object like this:



df = pd.read_csv('input.csv')
Name ID Title Date Prj1_Assigned
0 Joshua Morales MF6B9X Tech_Rep 08-Nov-2016 948
1 Betty García ERTW77 SME 08-Nov-2016 965
2 Kathleen Marrero KTD684 Probation 08-Nov-2016 946
3 Mark León GSL89D Tech_Rep 08-Nov-2016 951

Prj1_closed Prj2_assigned Prj2_solved
0 740 8 8
1 854 15 12
2 948 na na
3 844 6 4


If you just select KTD684 and GSL89D:



id_list = ['KTD684', 'GSL89D']
df = df[df['ID'].isin(id_list)]
Name ID Title Date Prj1_Assigned
2 Kathleen Marrero KTD684 Probation 08-Nov-2016 946
3 Mark León GSL89D Tech_Rep 08-Nov-2016 951

Prj1_closed Prj2_assigned Prj2_solved
2 948 na na
3 844 6 4


The groupby operation groups on ID and export each unique ID to a CSV file resulting in:



KTD684.csv
Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,946,948,na,na

GSL89D.csv
Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,951,844,6,4






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 18:14

























answered Nov 14 '18 at 14:23









AlexAlex

925721




925721












  • Dear Alex, thanks so much for offering your take on this.. I find the following error please File "<ipython-input-5-e4be53771aeb>", line 4 header=["Date", "Prj1_Assigned", "Prj1_closed", ^ SyntaxError: keyword argument repeated [This could be something minor, or maybe even my mistake, but I've been hustling with this since a while]

    – Lokkii9
    Nov 14 '18 at 17:39











  • I’ve updated the answer, I accidentally duplicated a parameter.

    – Alex
    Nov 14 '18 at 18:15











  • Thankss a tonn Alex...It really worked well... :) :) :)

    – Lokkii9
    Nov 14 '18 at 19:30











  • I'm glad it worked, take a look at the pandas docs to get an idea of what you can do with the library. If you get stuck you can ask at the pandas tag.

    – Alex
    Nov 14 '18 at 22:11

















  • Dear Alex, thanks so much for offering your take on this.. I find the following error please File "<ipython-input-5-e4be53771aeb>", line 4 header=["Date", "Prj1_Assigned", "Prj1_closed", ^ SyntaxError: keyword argument repeated [This could be something minor, or maybe even my mistake, but I've been hustling with this since a while]

    – Lokkii9
    Nov 14 '18 at 17:39











  • I’ve updated the answer, I accidentally duplicated a parameter.

    – Alex
    Nov 14 '18 at 18:15











  • Thankss a tonn Alex...It really worked well... :) :) :)

    – Lokkii9
    Nov 14 '18 at 19:30











  • I'm glad it worked, take a look at the pandas docs to get an idea of what you can do with the library. If you get stuck you can ask at the pandas tag.

    – Alex
    Nov 14 '18 at 22:11
















Dear Alex, thanks so much for offering your take on this.. I find the following error please File "<ipython-input-5-e4be53771aeb>", line 4 header=["Date", "Prj1_Assigned", "Prj1_closed", ^ SyntaxError: keyword argument repeated [This could be something minor, or maybe even my mistake, but I've been hustling with this since a while]

– Lokkii9
Nov 14 '18 at 17:39





Dear Alex, thanks so much for offering your take on this.. I find the following error please File "<ipython-input-5-e4be53771aeb>", line 4 header=["Date", "Prj1_Assigned", "Prj1_closed", ^ SyntaxError: keyword argument repeated [This could be something minor, or maybe even my mistake, but I've been hustling with this since a while]

– Lokkii9
Nov 14 '18 at 17:39













I’ve updated the answer, I accidentally duplicated a parameter.

– Alex
Nov 14 '18 at 18:15





I’ve updated the answer, I accidentally duplicated a parameter.

– Alex
Nov 14 '18 at 18:15













Thankss a tonn Alex...It really worked well... :) :) :)

– Lokkii9
Nov 14 '18 at 19:30





Thankss a tonn Alex...It really worked well... :) :) :)

– Lokkii9
Nov 14 '18 at 19:30













I'm glad it worked, take a look at the pandas docs to get an idea of what you can do with the library. If you get stuck you can ask at the pandas tag.

– Alex
Nov 14 '18 at 22:11





I'm glad it worked, take a look at the pandas docs to get an idea of what you can do with the library. If you get stuck you can ask at the pandas tag.

– Alex
Nov 14 '18 at 22:11













1














If I understand your problem correctly; You need to read from 2 input files:



  • 1 containing the users IDs you are looking for


  • 2 containing the project data related to users


In that fashion something like this would find all the users you specify in 1 in file 2 and write them out to result.csv




Sepicify your search IDs in search_for.csv. Keep in mind that this
will revrite your result.csv every time you run it.




import csv
import sys
import os


inputPatterns = open(os.curdir + '/search_for.csv', 'rt')

# Reader for the IDs (users) you are looking to find (key)
reader = csv.reader(inputPatterns)

ids =

# reading the IDs you are looking for from search_for.csv
for row in reader:
ids.append(row[0])
inputPatterns.close()

# Let's see if any of the user IDs we are looking for has any project related info
# if so write them to your output CSV
for userID in ids:
# Organization list with names and Company ID and reader
userList = open(os.curdir + '/users.csv', 'rt')
reader = csv.reader(userList)

# This will be the output file
result_f = open(os.curdir + "/" + userID + ".csv", 'w')
w = csv.writer(result_f)
# Writing header information
w.writerow(['Date', 'Prj1_Assigned', 'Prj1_closed', 'Prj2_assigned', 'Prj2_solved'])

# Scanning for projects for user and appending them
for row in reader:
if userID == row[1]:
w.writerow([row[3], row[4], row[5], row[6], row[7]])
result_f.close()
userList.close()


For example, search_for.csv looks like this



if your search_for.csv looks like this






share|improve this answer

























  • Dear Gabor, thanks so very much for providing your solution on this. But when I execute the code, this gives me output files with the usernames as you had mentioned. Actually hoped it would be ID names(.csv), moreover, the output csv files, are only having the headers in them, but there are no data pertaining to them MF6B9X,Tech_Rep, 08-Nov-2016,948,740,8,8 ... kindly help me with the steps of: [1]Opening Master file(Trials.csv) - [2]Finding the exact column where 'KTD684' is available - [3]Appending the(already existing KTD684.csv with the 5 values from Date... in the next available row pls

    – Lokkii9
    Nov 14 '18 at 17:55











  • Hi Lokkii, Yes I misunderstood your question in the first version. This version was uploaded before your comment and should do exactly what you are looking for. I'm also new to StackOverflow so I didn't know that I need to notify you on update :) But just to clarify. If you use input files: search_for.csv and users.csv (instead of Trials.csv) in the same directory as this python file. It should work as desired; Creates useID.csv files. I see you already accepted an answer so the problem is solved. One note: This problem can be solved in excel pivot table very quickly.

    – Gábor Hertelendy
    Nov 15 '18 at 3:28












  • Hi Gabor :) thanks soo very much for this..It does work perfectly well... :) Appreciate your time!

    – Lokkii9
    Nov 15 '18 at 16:24















1














If I understand your problem correctly; You need to read from 2 input files:



  • 1 containing the users IDs you are looking for


  • 2 containing the project data related to users


In that fashion something like this would find all the users you specify in 1 in file 2 and write them out to result.csv




Sepicify your search IDs in search_for.csv. Keep in mind that this
will revrite your result.csv every time you run it.




import csv
import sys
import os


inputPatterns = open(os.curdir + '/search_for.csv', 'rt')

# Reader for the IDs (users) you are looking to find (key)
reader = csv.reader(inputPatterns)

ids =

# reading the IDs you are looking for from search_for.csv
for row in reader:
ids.append(row[0])
inputPatterns.close()

# Let's see if any of the user IDs we are looking for has any project related info
# if so write them to your output CSV
for userID in ids:
# Organization list with names and Company ID and reader
userList = open(os.curdir + '/users.csv', 'rt')
reader = csv.reader(userList)

# This will be the output file
result_f = open(os.curdir + "/" + userID + ".csv", 'w')
w = csv.writer(result_f)
# Writing header information
w.writerow(['Date', 'Prj1_Assigned', 'Prj1_closed', 'Prj2_assigned', 'Prj2_solved'])

# Scanning for projects for user and appending them
for row in reader:
if userID == row[1]:
w.writerow([row[3], row[4], row[5], row[6], row[7]])
result_f.close()
userList.close()


For example, search_for.csv looks like this



if your search_for.csv looks like this






share|improve this answer

























  • Dear Gabor, thanks so very much for providing your solution on this. But when I execute the code, this gives me output files with the usernames as you had mentioned. Actually hoped it would be ID names(.csv), moreover, the output csv files, are only having the headers in them, but there are no data pertaining to them MF6B9X,Tech_Rep, 08-Nov-2016,948,740,8,8 ... kindly help me with the steps of: [1]Opening Master file(Trials.csv) - [2]Finding the exact column where 'KTD684' is available - [3]Appending the(already existing KTD684.csv with the 5 values from Date... in the next available row pls

    – Lokkii9
    Nov 14 '18 at 17:55











  • Hi Lokkii, Yes I misunderstood your question in the first version. This version was uploaded before your comment and should do exactly what you are looking for. I'm also new to StackOverflow so I didn't know that I need to notify you on update :) But just to clarify. If you use input files: search_for.csv and users.csv (instead of Trials.csv) in the same directory as this python file. It should work as desired; Creates useID.csv files. I see you already accepted an answer so the problem is solved. One note: This problem can be solved in excel pivot table very quickly.

    – Gábor Hertelendy
    Nov 15 '18 at 3:28












  • Hi Gabor :) thanks soo very much for this..It does work perfectly well... :) Appreciate your time!

    – Lokkii9
    Nov 15 '18 at 16:24













1












1








1







If I understand your problem correctly; You need to read from 2 input files:



  • 1 containing the users IDs you are looking for


  • 2 containing the project data related to users


In that fashion something like this would find all the users you specify in 1 in file 2 and write them out to result.csv




Sepicify your search IDs in search_for.csv. Keep in mind that this
will revrite your result.csv every time you run it.




import csv
import sys
import os


inputPatterns = open(os.curdir + '/search_for.csv', 'rt')

# Reader for the IDs (users) you are looking to find (key)
reader = csv.reader(inputPatterns)

ids =

# reading the IDs you are looking for from search_for.csv
for row in reader:
ids.append(row[0])
inputPatterns.close()

# Let's see if any of the user IDs we are looking for has any project related info
# if so write them to your output CSV
for userID in ids:
# Organization list with names and Company ID and reader
userList = open(os.curdir + '/users.csv', 'rt')
reader = csv.reader(userList)

# This will be the output file
result_f = open(os.curdir + "/" + userID + ".csv", 'w')
w = csv.writer(result_f)
# Writing header information
w.writerow(['Date', 'Prj1_Assigned', 'Prj1_closed', 'Prj2_assigned', 'Prj2_solved'])

# Scanning for projects for user and appending them
for row in reader:
if userID == row[1]:
w.writerow([row[3], row[4], row[5], row[6], row[7]])
result_f.close()
userList.close()


For example, search_for.csv looks like this



if your search_for.csv looks like this






share|improve this answer















If I understand your problem correctly; You need to read from 2 input files:



  • 1 containing the users IDs you are looking for


  • 2 containing the project data related to users


In that fashion something like this would find all the users you specify in 1 in file 2 and write them out to result.csv




Sepicify your search IDs in search_for.csv. Keep in mind that this
will revrite your result.csv every time you run it.




import csv
import sys
import os


inputPatterns = open(os.curdir + '/search_for.csv', 'rt')

# Reader for the IDs (users) you are looking to find (key)
reader = csv.reader(inputPatterns)

ids =

# reading the IDs you are looking for from search_for.csv
for row in reader:
ids.append(row[0])
inputPatterns.close()

# Let's see if any of the user IDs we are looking for has any project related info
# if so write them to your output CSV
for userID in ids:
# Organization list with names and Company ID and reader
userList = open(os.curdir + '/users.csv', 'rt')
reader = csv.reader(userList)

# This will be the output file
result_f = open(os.curdir + "/" + userID + ".csv", 'w')
w = csv.writer(result_f)
# Writing header information
w.writerow(['Date', 'Prj1_Assigned', 'Prj1_closed', 'Prj2_assigned', 'Prj2_solved'])

# Scanning for projects for user and appending them
for row in reader:
if userID == row[1]:
w.writerow([row[3], row[4], row[5], row[6], row[7]])
result_f.close()
userList.close()


For example, search_for.csv looks like this



if your search_for.csv looks like this







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 16:04

























answered Nov 14 '18 at 14:58









Gábor HertelendyGábor Hertelendy

113




113












  • Dear Gabor, thanks so very much for providing your solution on this. But when I execute the code, this gives me output files with the usernames as you had mentioned. Actually hoped it would be ID names(.csv), moreover, the output csv files, are only having the headers in them, but there are no data pertaining to them MF6B9X,Tech_Rep, 08-Nov-2016,948,740,8,8 ... kindly help me with the steps of: [1]Opening Master file(Trials.csv) - [2]Finding the exact column where 'KTD684' is available - [3]Appending the(already existing KTD684.csv with the 5 values from Date... in the next available row pls

    – Lokkii9
    Nov 14 '18 at 17:55











  • Hi Lokkii, Yes I misunderstood your question in the first version. This version was uploaded before your comment and should do exactly what you are looking for. I'm also new to StackOverflow so I didn't know that I need to notify you on update :) But just to clarify. If you use input files: search_for.csv and users.csv (instead of Trials.csv) in the same directory as this python file. It should work as desired; Creates useID.csv files. I see you already accepted an answer so the problem is solved. One note: This problem can be solved in excel pivot table very quickly.

    – Gábor Hertelendy
    Nov 15 '18 at 3:28












  • Hi Gabor :) thanks soo very much for this..It does work perfectly well... :) Appreciate your time!

    – Lokkii9
    Nov 15 '18 at 16:24

















  • Dear Gabor, thanks so very much for providing your solution on this. But when I execute the code, this gives me output files with the usernames as you had mentioned. Actually hoped it would be ID names(.csv), moreover, the output csv files, are only having the headers in them, but there are no data pertaining to them MF6B9X,Tech_Rep, 08-Nov-2016,948,740,8,8 ... kindly help me with the steps of: [1]Opening Master file(Trials.csv) - [2]Finding the exact column where 'KTD684' is available - [3]Appending the(already existing KTD684.csv with the 5 values from Date... in the next available row pls

    – Lokkii9
    Nov 14 '18 at 17:55











  • Hi Lokkii, Yes I misunderstood your question in the first version. This version was uploaded before your comment and should do exactly what you are looking for. I'm also new to StackOverflow so I didn't know that I need to notify you on update :) But just to clarify. If you use input files: search_for.csv and users.csv (instead of Trials.csv) in the same directory as this python file. It should work as desired; Creates useID.csv files. I see you already accepted an answer so the problem is solved. One note: This problem can be solved in excel pivot table very quickly.

    – Gábor Hertelendy
    Nov 15 '18 at 3:28












  • Hi Gabor :) thanks soo very much for this..It does work perfectly well... :) Appreciate your time!

    – Lokkii9
    Nov 15 '18 at 16:24
















Dear Gabor, thanks so very much for providing your solution on this. But when I execute the code, this gives me output files with the usernames as you had mentioned. Actually hoped it would be ID names(.csv), moreover, the output csv files, are only having the headers in them, but there are no data pertaining to them MF6B9X,Tech_Rep, 08-Nov-2016,948,740,8,8 ... kindly help me with the steps of: [1]Opening Master file(Trials.csv) - [2]Finding the exact column where 'KTD684' is available - [3]Appending the(already existing KTD684.csv with the 5 values from Date... in the next available row pls

– Lokkii9
Nov 14 '18 at 17:55





Dear Gabor, thanks so very much for providing your solution on this. But when I execute the code, this gives me output files with the usernames as you had mentioned. Actually hoped it would be ID names(.csv), moreover, the output csv files, are only having the headers in them, but there are no data pertaining to them MF6B9X,Tech_Rep, 08-Nov-2016,948,740,8,8 ... kindly help me with the steps of: [1]Opening Master file(Trials.csv) - [2]Finding the exact column where 'KTD684' is available - [3]Appending the(already existing KTD684.csv with the 5 values from Date... in the next available row pls

– Lokkii9
Nov 14 '18 at 17:55













Hi Lokkii, Yes I misunderstood your question in the first version. This version was uploaded before your comment and should do exactly what you are looking for. I'm also new to StackOverflow so I didn't know that I need to notify you on update :) But just to clarify. If you use input files: search_for.csv and users.csv (instead of Trials.csv) in the same directory as this python file. It should work as desired; Creates useID.csv files. I see you already accepted an answer so the problem is solved. One note: This problem can be solved in excel pivot table very quickly.

– Gábor Hertelendy
Nov 15 '18 at 3:28






Hi Lokkii, Yes I misunderstood your question in the first version. This version was uploaded before your comment and should do exactly what you are looking for. I'm also new to StackOverflow so I didn't know that I need to notify you on update :) But just to clarify. If you use input files: search_for.csv and users.csv (instead of Trials.csv) in the same directory as this python file. It should work as desired; Creates useID.csv files. I see you already accepted an answer so the problem is solved. One note: This problem can be solved in excel pivot table very quickly.

– Gábor Hertelendy
Nov 15 '18 at 3:28














Hi Gabor :) thanks soo very much for this..It does work perfectly well... :) Appreciate your time!

– Lokkii9
Nov 15 '18 at 16:24





Hi Gabor :) thanks soo very much for this..It does work perfectly well... :) Appreciate your time!

– Lokkii9
Nov 15 '18 at 16:24











0














Here's a pure python approach which reads the master .csv file with csv.DictReader, matches the ids, and appends the file data into a new or existing .csv file with csv.DictWriter():



from csv import DictReader
from csv import DictWriter

from os.path import isfile

def export_csv(user_id, master_csv, fieldnames, key_id, extension=".csv"):
filename = user_id + extension
file_exists = isfile(filename)

with open(file=master_csv) as in_file, open(
file=filename, mode="a", newline=""
) as out_file:

# Create reading and writing objects
csv_reader = DictReader(in_file)
csv_writer = DictWriter(out_file, fieldnames=fieldnames)

# Only write header once
if not file_exists:
csv_writer.writeheader()

# Go through lines and match ids
for line in csv_reader:
if line[key_id] == user_id:

# Modify line and append to file
line = k: v.strip() for k, v in line.items() if k in fieldnames
csv_writer.writerow(line)


Which can be called like this:



export_csv(
user_id="KTD684",
master_csv="master.csv",
fieldnames=["Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned", "Prj2_solved"],
key_id="ID",
)


And produces the following KTD684.csv:



Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,946,948,na,na





share|improve this answer

























  • Dear RoadRunner, thanks really for taking time on this one..kindly find this error which seems to be coming when I execute this. **TypeError Traceback (most recent call last) <ipython-input-7-3ae7e09af9ad> in <module>() 3 master_csv="master.csv", 4 columns=["Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned", "Prj2_solved"], ----> 5 key_id="ID", 6 ) TypeError: export_csv() got an unexpected keyword argument 'columns' ** Is there any way around for this please..

    – Lokkii9
    Nov 14 '18 at 18:05











  • @Lokkii9 No problem. I updated the answer.

    – RoadRunner
    Nov 15 '18 at 1:13











  • Very Grateful for this RoadRunner...sorry for the late response... :) :) :)

    – Lokkii9
    Nov 17 '18 at 15:42















0














Here's a pure python approach which reads the master .csv file with csv.DictReader, matches the ids, and appends the file data into a new or existing .csv file with csv.DictWriter():



from csv import DictReader
from csv import DictWriter

from os.path import isfile

def export_csv(user_id, master_csv, fieldnames, key_id, extension=".csv"):
filename = user_id + extension
file_exists = isfile(filename)

with open(file=master_csv) as in_file, open(
file=filename, mode="a", newline=""
) as out_file:

# Create reading and writing objects
csv_reader = DictReader(in_file)
csv_writer = DictWriter(out_file, fieldnames=fieldnames)

# Only write header once
if not file_exists:
csv_writer.writeheader()

# Go through lines and match ids
for line in csv_reader:
if line[key_id] == user_id:

# Modify line and append to file
line = k: v.strip() for k, v in line.items() if k in fieldnames
csv_writer.writerow(line)


Which can be called like this:



export_csv(
user_id="KTD684",
master_csv="master.csv",
fieldnames=["Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned", "Prj2_solved"],
key_id="ID",
)


And produces the following KTD684.csv:



Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,946,948,na,na





share|improve this answer

























  • Dear RoadRunner, thanks really for taking time on this one..kindly find this error which seems to be coming when I execute this. **TypeError Traceback (most recent call last) <ipython-input-7-3ae7e09af9ad> in <module>() 3 master_csv="master.csv", 4 columns=["Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned", "Prj2_solved"], ----> 5 key_id="ID", 6 ) TypeError: export_csv() got an unexpected keyword argument 'columns' ** Is there any way around for this please..

    – Lokkii9
    Nov 14 '18 at 18:05











  • @Lokkii9 No problem. I updated the answer.

    – RoadRunner
    Nov 15 '18 at 1:13











  • Very Grateful for this RoadRunner...sorry for the late response... :) :) :)

    – Lokkii9
    Nov 17 '18 at 15:42













0












0








0







Here's a pure python approach which reads the master .csv file with csv.DictReader, matches the ids, and appends the file data into a new or existing .csv file with csv.DictWriter():



from csv import DictReader
from csv import DictWriter

from os.path import isfile

def export_csv(user_id, master_csv, fieldnames, key_id, extension=".csv"):
filename = user_id + extension
file_exists = isfile(filename)

with open(file=master_csv) as in_file, open(
file=filename, mode="a", newline=""
) as out_file:

# Create reading and writing objects
csv_reader = DictReader(in_file)
csv_writer = DictWriter(out_file, fieldnames=fieldnames)

# Only write header once
if not file_exists:
csv_writer.writeheader()

# Go through lines and match ids
for line in csv_reader:
if line[key_id] == user_id:

# Modify line and append to file
line = k: v.strip() for k, v in line.items() if k in fieldnames
csv_writer.writerow(line)


Which can be called like this:



export_csv(
user_id="KTD684",
master_csv="master.csv",
fieldnames=["Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned", "Prj2_solved"],
key_id="ID",
)


And produces the following KTD684.csv:



Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,946,948,na,na





share|improve this answer















Here's a pure python approach which reads the master .csv file with csv.DictReader, matches the ids, and appends the file data into a new or existing .csv file with csv.DictWriter():



from csv import DictReader
from csv import DictWriter

from os.path import isfile

def export_csv(user_id, master_csv, fieldnames, key_id, extension=".csv"):
filename = user_id + extension
file_exists = isfile(filename)

with open(file=master_csv) as in_file, open(
file=filename, mode="a", newline=""
) as out_file:

# Create reading and writing objects
csv_reader = DictReader(in_file)
csv_writer = DictWriter(out_file, fieldnames=fieldnames)

# Only write header once
if not file_exists:
csv_writer.writeheader()

# Go through lines and match ids
for line in csv_reader:
if line[key_id] == user_id:

# Modify line and append to file
line = k: v.strip() for k, v in line.items() if k in fieldnames
csv_writer.writerow(line)


Which can be called like this:



export_csv(
user_id="KTD684",
master_csv="master.csv",
fieldnames=["Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned", "Prj2_solved"],
key_id="ID",
)


And produces the following KTD684.csv:



Date,Prj1_Assigned,Prj1_closed,Prj2_assigned,Prj2_solved
08-Nov-2016,946,948,na,na






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 1:13

























answered Nov 14 '18 at 14:38









RoadRunnerRoadRunner

11.2k31340




11.2k31340












  • Dear RoadRunner, thanks really for taking time on this one..kindly find this error which seems to be coming when I execute this. **TypeError Traceback (most recent call last) <ipython-input-7-3ae7e09af9ad> in <module>() 3 master_csv="master.csv", 4 columns=["Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned", "Prj2_solved"], ----> 5 key_id="ID", 6 ) TypeError: export_csv() got an unexpected keyword argument 'columns' ** Is there any way around for this please..

    – Lokkii9
    Nov 14 '18 at 18:05











  • @Lokkii9 No problem. I updated the answer.

    – RoadRunner
    Nov 15 '18 at 1:13











  • Very Grateful for this RoadRunner...sorry for the late response... :) :) :)

    – Lokkii9
    Nov 17 '18 at 15:42

















  • Dear RoadRunner, thanks really for taking time on this one..kindly find this error which seems to be coming when I execute this. **TypeError Traceback (most recent call last) <ipython-input-7-3ae7e09af9ad> in <module>() 3 master_csv="master.csv", 4 columns=["Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned", "Prj2_solved"], ----> 5 key_id="ID", 6 ) TypeError: export_csv() got an unexpected keyword argument 'columns' ** Is there any way around for this please..

    – Lokkii9
    Nov 14 '18 at 18:05











  • @Lokkii9 No problem. I updated the answer.

    – RoadRunner
    Nov 15 '18 at 1:13











  • Very Grateful for this RoadRunner...sorry for the late response... :) :) :)

    – Lokkii9
    Nov 17 '18 at 15:42
















Dear RoadRunner, thanks really for taking time on this one..kindly find this error which seems to be coming when I execute this. **TypeError Traceback (most recent call last) <ipython-input-7-3ae7e09af9ad> in <module>() 3 master_csv="master.csv", 4 columns=["Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned", "Prj2_solved"], ----> 5 key_id="ID", 6 ) TypeError: export_csv() got an unexpected keyword argument 'columns' ** Is there any way around for this please..

– Lokkii9
Nov 14 '18 at 18:05





Dear RoadRunner, thanks really for taking time on this one..kindly find this error which seems to be coming when I execute this. **TypeError Traceback (most recent call last) <ipython-input-7-3ae7e09af9ad> in <module>() 3 master_csv="master.csv", 4 columns=["Date", "Prj1_Assigned", "Prj1_closed", "Prj2_assigned", "Prj2_solved"], ----> 5 key_id="ID", 6 ) TypeError: export_csv() got an unexpected keyword argument 'columns' ** Is there any way around for this please..

– Lokkii9
Nov 14 '18 at 18:05













@Lokkii9 No problem. I updated the answer.

– RoadRunner
Nov 15 '18 at 1:13





@Lokkii9 No problem. I updated the answer.

– RoadRunner
Nov 15 '18 at 1:13













Very Grateful for this RoadRunner...sorry for the late response... :) :) :)

– Lokkii9
Nov 17 '18 at 15:42





Very Grateful for this RoadRunner...sorry for the late response... :) :) :)

– Lokkii9
Nov 17 '18 at 15:42

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302202%2fpython-numpycsv-finding-values-appending-another-csv%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3