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.

Boris Ivanov

Testing Team Lead

7 年

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

Du?an ?u?i?

Director, Cloud Infrastructure & InfoSec | Expert in Cloud Solutions | System Design | DevOps | Compliance | Observability | Automation | Scalability

7 年

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

回复

要查看或添加评论,请登录

Boris Ivanov的更多文章

  • Google Chrome extension Swagger model / schema beautifier

    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

    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

    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

    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…

    1 条评论
  • Delete old files from directory in batch

    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…

社区洞察

其他会员也浏览了