Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

I am trying to learn Python?

I want to read a csv file that has two column headings - name and score. I want python to print only the scores that are <50. How to I go about this? Please help, I have searched and search and cant make sense of the help.

Update:

I am opening a file from excel, it is saved as examscores.csv and it has two columns with headings name and score.

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    If with "two column headings", you mean that the file is a plain text file consisting of two columns of the format "name" and "score", you would do it that way:

    First, you open the file in read-only mode:

    Python: csvFile = open(file)

    You then create a new list:

    Python: fileLineList=[]

    Then you loop over the lines of the file, and add each line to the list:

    Python:

    for line in svgFile:

    fileLineList.append(line)

    then you loop over the list, evaluate the scores and then print theappropriatee strings out:

    for line in fileLineList:

    strings = line.split(" ") //this works only if the name and score are seperated by exactly one space

    if float(strings[2]) >49:

    print line

    I didn't try it out, but it should work like that. Make sure to add indentions (yahoo doesn't support whitespace).

Still have questions? Get your answers by asking now.