Read csv file

Read csv file

This is a short script which just reads an .csv file where the user inputs the number of the line or value you want to read from the csv file and afterwards clean (delete) that value from the csv file. The code for this script is the following:

#Author: Grimy88
def getSelectedValue(number):
    f = open("yourFileName.csv", "r")
    myList = []
    myString = ""
    endingString = ""
    for line in f:    
        myString = line
    f.close()
    myList = myString.split(",")
    f1 = open("resultFileName", "w")
    f1.write(myList[number])
    f1.close()
    myList.remove(myList[number])
    endingString = endingString.join(str(e+", ") for e in myList)
    f = open("yourFileName.csv", "w")
    f.write(endingString)
    f.close()
    

#Code Example
getSelectedValue(4)

In this code we take the value and write it into another file but you can easily change it to not write to a new file rather just return that value if you need it. Feel free to change the script how you see fit and how you need it. Remark currently the filename is hard-coded but you can change it so that the first parameter is the filename.

Yes i agree, it can easily be changed i thought i wrote it in the description :)

Maybe it's better to pass filename as 1st arg, not to hard-code it. :)

Like
Reply

To view or add a comment, sign in

More articles by Boris Ivanov

  • Google Chrome extension Swagger model / schema beautifier

    This Google Chrome Extension will beautify the model or schema in Swagger call details table (depends on how you…

  • Helper functions in python

    Here are helper functions to clean folder and to create a directory (folder) if it does not exist on a specific…

  • Parse XML - Search for element and its value in Python

    This method is used to search for element inside xml file and its value, depending on the type of value we are doing…

  • Change registry value in python

    If you need to change the registry value on Windows you can use this short script to automate the process easily. In…

  • Delete old files from directory in batch

    If you have an folder in which you have lots of files and its too hard to manually delete them one by one, for that…

Explore content categories