+ 2
CSV files
How would you edit the details of a runner e.g. if they did another race and got a better run time, you can type their new time and it will show, and being able to remove them from the CSV file without using the CSV module and others e.g. if they were disqualified from the race, you would need to remove them. https://code.sololearn.com/cvkZ4jY92bCX/?ref=app At the moment I can add and search for a runner
7 Respostas
+ 3
Modifying a file is a tricky business. There is no built-in way to insert or modify the contents of a file that you are reading from. You can use temp files for writing, as described here:
https://stackoverflow.com/questions/16020858/inline-csv-file-editing-with-JUMP_LINK__&&__python__&&__JUMP_LINK
But I would rather recommend a database approach if you need to modify the data. Sqlite3 is not too difficult to use, and it is built in to python. In fact it creates a file to store the data that you can access and change with sql statements.
See an example :
https://code.sololearn.com/csZ5bjiWfdCR/?ref=app
+ 2
I just spent a minute playing with what you had and think you can get there from where I left it.
this is your change_runner function edited to what I think will work
Obviously not finishied
if this does not help you can shoot me a message and i can finish with where i was going. Good Luck
def change_runner():
try:
infile = open('marathon.csv', 'w') # open file with write
outfile = '' #this will hold what is read
runner=input('Enter the name of the runner to edit: ')
line = readline(infile) #read the first line of the time
while line != '': #if the lines not empty
if runner in line:
# in this part of the loop we have the line of the file that contains the name and time. you can display it or break it into elements or whatever you want to do. just make sure to write it to the outfile before leaving here so that it can be written after iterating the entire file.
else:
#else the line doesnt contain our runner. assign it to a variable to hold it.
outfile = outfile + line + '\n'
infile.write(outfile)
infile.close()
except IOError:
#handle except here
print('Action completed!')
+ 1
thanks Tibor ill see if that works, but im trying to change the data without importing any modules e.g. not importing CSV and using the writer and reader functions😓
+ 1
Thanks Matt, i appreciate you taking the time to answer my question. ill see if i can make it work to modify the details, if you dont mind could you finish the code😅
+ 1
Why do you not want to import anything? Doing it without using 'import os' gets tricky
+ 1
I take that back. here is a delete_runner() function that deletes data. Minor modification to it should allow your update things in the file. (reference what I had said earlier on iterating over the line)
def delete_runner():
try:
infile = open('marathon.csv','r')
runner=input('Enter the name of the runner you would like to remove: ')
#outfile to store the modified data
outfile = []
#priming read
line = infile.readline()
while line != '':
if runner in line:
line = infile.readline()
else: #in a try catch to help trouble shoot
try:
outfile.append(line)
line = infile.readline()
except Exception as err:
print(err)
# close file object for read
infile.close()
#open file object for write
infile = open('marathon.csv', 'w')
#iterate over new list and write to the file
for items in outfile:
infile.write(items)
#close the file
infile.close()
except IOError:
print('There was a problem reading the file')
+ 1
its for a mini project where i. was trying to build a database but not use any modules, ik it would be easier but i was trying to give myself a challenge and got stuck😓. Thanks Matt