How to get certain values from a text file in python 3










-2















This is the text file I have:



#EMP_NO, EMP_NAME, AGE, POSITION, SALARY, YRS_EMP 
001, Peter Smyth, 26, Developer, 29000, 4
002, Samuel Jones, 23, Developer, 24000, 1
003, Laura Stewart, 41, DevOps, 42000, 15
004, Paul Jones, 24, Analyst, 21000, 2
005, Simon Brown, 52, Developer, 53000, 18
006, George Staples, 42, Tester, 42000, 12
007, Greg Throne, 57, DevOps, 50000, 23
008, Aston Bently, 27, Tester, 33000, 5
009, Ben Evans, 32, DevOps, 38000, 2
010, Emma Samson, 23, DevOps, 22000, 1
011, Stephanie Beggs, 43, Tester, 19000, 9
012, Sarah McQuillin, 47, DevOps, 23000, 5
013, Grace Corrigan, 48, Analyst, 44000, 16
014, Simone Mills, 32, DevOps, 32000, 11
015, Martin Montgomery, 28, Analyst, 28000, 3


I need to get all the salaries and add them together but I can only get the whole line returned, any ideas how to do this in python 3?










share|improve this question



















  • 2





    What have you tried so far?

    – Klaus D.
    Nov 13 '18 at 16:15











  • Possible duplicate of Read specific columns from a csv file with csv module?

    – colidyre
    Nov 13 '18 at 16:19















-2















This is the text file I have:



#EMP_NO, EMP_NAME, AGE, POSITION, SALARY, YRS_EMP 
001, Peter Smyth, 26, Developer, 29000, 4
002, Samuel Jones, 23, Developer, 24000, 1
003, Laura Stewart, 41, DevOps, 42000, 15
004, Paul Jones, 24, Analyst, 21000, 2
005, Simon Brown, 52, Developer, 53000, 18
006, George Staples, 42, Tester, 42000, 12
007, Greg Throne, 57, DevOps, 50000, 23
008, Aston Bently, 27, Tester, 33000, 5
009, Ben Evans, 32, DevOps, 38000, 2
010, Emma Samson, 23, DevOps, 22000, 1
011, Stephanie Beggs, 43, Tester, 19000, 9
012, Sarah McQuillin, 47, DevOps, 23000, 5
013, Grace Corrigan, 48, Analyst, 44000, 16
014, Simone Mills, 32, DevOps, 32000, 11
015, Martin Montgomery, 28, Analyst, 28000, 3


I need to get all the salaries and add them together but I can only get the whole line returned, any ideas how to do this in python 3?










share|improve this question



















  • 2





    What have you tried so far?

    – Klaus D.
    Nov 13 '18 at 16:15











  • Possible duplicate of Read specific columns from a csv file with csv module?

    – colidyre
    Nov 13 '18 at 16:19













-2












-2








-2








This is the text file I have:



#EMP_NO, EMP_NAME, AGE, POSITION, SALARY, YRS_EMP 
001, Peter Smyth, 26, Developer, 29000, 4
002, Samuel Jones, 23, Developer, 24000, 1
003, Laura Stewart, 41, DevOps, 42000, 15
004, Paul Jones, 24, Analyst, 21000, 2
005, Simon Brown, 52, Developer, 53000, 18
006, George Staples, 42, Tester, 42000, 12
007, Greg Throne, 57, DevOps, 50000, 23
008, Aston Bently, 27, Tester, 33000, 5
009, Ben Evans, 32, DevOps, 38000, 2
010, Emma Samson, 23, DevOps, 22000, 1
011, Stephanie Beggs, 43, Tester, 19000, 9
012, Sarah McQuillin, 47, DevOps, 23000, 5
013, Grace Corrigan, 48, Analyst, 44000, 16
014, Simone Mills, 32, DevOps, 32000, 11
015, Martin Montgomery, 28, Analyst, 28000, 3


I need to get all the salaries and add them together but I can only get the whole line returned, any ideas how to do this in python 3?










share|improve this question
















This is the text file I have:



#EMP_NO, EMP_NAME, AGE, POSITION, SALARY, YRS_EMP 
001, Peter Smyth, 26, Developer, 29000, 4
002, Samuel Jones, 23, Developer, 24000, 1
003, Laura Stewart, 41, DevOps, 42000, 15
004, Paul Jones, 24, Analyst, 21000, 2
005, Simon Brown, 52, Developer, 53000, 18
006, George Staples, 42, Tester, 42000, 12
007, Greg Throne, 57, DevOps, 50000, 23
008, Aston Bently, 27, Tester, 33000, 5
009, Ben Evans, 32, DevOps, 38000, 2
010, Emma Samson, 23, DevOps, 22000, 1
011, Stephanie Beggs, 43, Tester, 19000, 9
012, Sarah McQuillin, 47, DevOps, 23000, 5
013, Grace Corrigan, 48, Analyst, 44000, 16
014, Simone Mills, 32, DevOps, 32000, 11
015, Martin Montgomery, 28, Analyst, 28000, 3


I need to get all the salaries and add them together but I can only get the whole line returned, any ideas how to do this in python 3?







python python-3.x file csv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 17:04









Patrick Artner

23.1k62343




23.1k62343










asked Nov 13 '18 at 16:13









Ross CurrieRoss Currie

1




1







  • 2





    What have you tried so far?

    – Klaus D.
    Nov 13 '18 at 16:15











  • Possible duplicate of Read specific columns from a csv file with csv module?

    – colidyre
    Nov 13 '18 at 16:19












  • 2





    What have you tried so far?

    – Klaus D.
    Nov 13 '18 at 16:15











  • Possible duplicate of Read specific columns from a csv file with csv module?

    – colidyre
    Nov 13 '18 at 16:19







2




2





What have you tried so far?

– Klaus D.
Nov 13 '18 at 16:15





What have you tried so far?

– Klaus D.
Nov 13 '18 at 16:15













Possible duplicate of Read specific columns from a csv file with csv module?

– colidyre
Nov 13 '18 at 16:19





Possible duplicate of Read specific columns from a csv file with csv module?

– colidyre
Nov 13 '18 at 16:19












1 Answer
1






active

oldest

votes


















1














f = open('nameoffile.txt') #open file
sum = 0 #initialise
i = 0
for line in f: #read each line from file
line = line.split(',') #read line as string, split to list of strings at the commas
if(i>0): #to disregrad the first line that is the header
sum += float(line[-2]) #salary is second from last, convert string to float to add
i += 1

print("Sum = ", sum)





share|improve this answer

























  • Hello there. Your answer needs more explanation about how the code works to be a good answer.

    – Sergey Goliney
    Nov 13 '18 at 17:59






  • 1





    Done, see update m/

    – Keerthana Gopalakrishnan
    Nov 13 '18 at 18:02










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%2f53285131%2fhow-to-get-certain-values-from-a-text-file-in-python-3%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














f = open('nameoffile.txt') #open file
sum = 0 #initialise
i = 0
for line in f: #read each line from file
line = line.split(',') #read line as string, split to list of strings at the commas
if(i>0): #to disregrad the first line that is the header
sum += float(line[-2]) #salary is second from last, convert string to float to add
i += 1

print("Sum = ", sum)





share|improve this answer

























  • Hello there. Your answer needs more explanation about how the code works to be a good answer.

    – Sergey Goliney
    Nov 13 '18 at 17:59






  • 1





    Done, see update m/

    – Keerthana Gopalakrishnan
    Nov 13 '18 at 18:02















1














f = open('nameoffile.txt') #open file
sum = 0 #initialise
i = 0
for line in f: #read each line from file
line = line.split(',') #read line as string, split to list of strings at the commas
if(i>0): #to disregrad the first line that is the header
sum += float(line[-2]) #salary is second from last, convert string to float to add
i += 1

print("Sum = ", sum)





share|improve this answer

























  • Hello there. Your answer needs more explanation about how the code works to be a good answer.

    – Sergey Goliney
    Nov 13 '18 at 17:59






  • 1





    Done, see update m/

    – Keerthana Gopalakrishnan
    Nov 13 '18 at 18:02













1












1








1







f = open('nameoffile.txt') #open file
sum = 0 #initialise
i = 0
for line in f: #read each line from file
line = line.split(',') #read line as string, split to list of strings at the commas
if(i>0): #to disregrad the first line that is the header
sum += float(line[-2]) #salary is second from last, convert string to float to add
i += 1

print("Sum = ", sum)





share|improve this answer















f = open('nameoffile.txt') #open file
sum = 0 #initialise
i = 0
for line in f: #read each line from file
line = line.split(',') #read line as string, split to list of strings at the commas
if(i>0): #to disregrad the first line that is the header
sum += float(line[-2]) #salary is second from last, convert string to float to add
i += 1

print("Sum = ", sum)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 18:02

























answered Nov 13 '18 at 17:09









Keerthana GopalakrishnanKeerthana Gopalakrishnan

48769




48769












  • Hello there. Your answer needs more explanation about how the code works to be a good answer.

    – Sergey Goliney
    Nov 13 '18 at 17:59






  • 1





    Done, see update m/

    – Keerthana Gopalakrishnan
    Nov 13 '18 at 18:02

















  • Hello there. Your answer needs more explanation about how the code works to be a good answer.

    – Sergey Goliney
    Nov 13 '18 at 17:59






  • 1





    Done, see update m/

    – Keerthana Gopalakrishnan
    Nov 13 '18 at 18:02
















Hello there. Your answer needs more explanation about how the code works to be a good answer.

– Sergey Goliney
Nov 13 '18 at 17:59





Hello there. Your answer needs more explanation about how the code works to be a good answer.

– Sergey Goliney
Nov 13 '18 at 17:59




1




1





Done, see update m/

– Keerthana Gopalakrishnan
Nov 13 '18 at 18:02





Done, see update m/

– Keerthana Gopalakrishnan
Nov 13 '18 at 18:02

















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%2f53285131%2fhow-to-get-certain-values-from-a-text-file-in-python-3%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