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.
Testing Team Lead
7 年Yes i agree, it can easily be changed i thought i wrote it in the description :)
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. :)