Dec-23-2016, 04:53 PM
Hi everyone,
im quite new to python, especially to reading and writing files.
I now wrote a script which was intended to read a "csv utf-8" file, scan a code (as a normal keyboard-input), append the current time in the list and ovwerwrite the file with the new time. Since i couldn't find another way, I let the script overwrite the whole file.
But the script is having a big issue. Whenever I overwrite the file, stop and restart the script, the first line of the table gets deleted. It must happen anytime during the second read process because the file is intact after stopping the script and opening it with excel.
In hope that you will be able to help me,
lrxM
im quite new to python, especially to reading and writing files.
I now wrote a script which was intended to read a "csv utf-8" file, scan a code (as a normal keyboard-input), append the current time in the list and ovwerwrite the file with the new time. Since i couldn't find another way, I let the script overwrite the whole file.
But the script is having a big issue. Whenever I overwrite the file, stop and restart the script, the first line of the table gets deleted. It must happen anytime during the second read process because the file is intact after stopping the script and opening it with excel.
In hope that you will be able to help me,
lrxM
import csv #import plugins import time def writeTime(tableRead): print(tableRead[tempScan][1], tableRead[tempScan][0]) with open("G:\SaS\IT\Alle.csv","w", newline = "") as csvfile: print(tableRead[0]) presentTime = time.ctime(time.time())[11:-8] tableRead[tempScan].append(presentTime) tableRead.insert(0,"Schutzkappe") overwrite = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_MINIMAL) overwrite.writerows(tableRead) print(tableRead[0]) print(presentTime) print("Time was written successfully!") print() with open("G:\SaS\IT\Alle.csv") as csvfile: #file as variable, with closes the file in the memory after work is done read = csv.reader(csvfile, delimiter= ";") #defines the formatting, ";" seperates the single columms; quotechar isn't needed for a csv utf-8 files for row in read: #save the data into a list, acces with a[0][0] (applicable example) tableRead = list(read) print(tableRead[0]) while 1 == 1: print("Insert Barcode") tempScan = int(input()) #most barcodescanner work as a keyboard-input if tempScan <= len(tableRead): writeTime(tableRead) elif tempScan == 10001: break else: print("This is not a valid code!") print(tableRead[0]) csvfile.close() print("Program is shutting down!")