change encoding of csv, add BOM to csv, add option --excel for csv

This commit is contained in:
shrlnm 2023-06-01 22:12:45 +03:00
parent 8cb0852249
commit 4000072839
3 changed files with 39 additions and 14 deletions

View File

@ -412,7 +412,8 @@ class ECU:
if mod_globals.opt_csv and mod_globals.ext_cur_DTC == '000000':
# prepare to csv save
self.minimumrefreshrate = 0
#csvline = "sep=\\t\n"
if mod_globals.opt_excel:
csvline = "sep="+mod_globals.opt_csv_sep+"\n"
csvline = "Time"
nparams = 0
for dr in datarefs:
@ -425,7 +426,7 @@ class ECU:
if mod_globals.opt_usrkey: csvline += ";User events"
csvline = pyren_encode(csvline)
if nparams:
csv_start_time = datetime.now().timestamp()
csv_start_time = int(round(time.time() * 1000))
csv_filename = datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
#here the problem with russian letters. and very long name
csv_filename = csv_filename+'_'+self.ecudata['ecuname']+'_'+path
@ -436,7 +437,8 @@ class ECU:
csv_filename = csv_filename.replace(' ','_')
#if mod_globals.os == 'android':
# csv_filename = csv_filename.encode("ascii","ignore")
csvf = open("./csv/"+pyren_encode(csv_filename), "wb")
csvf = open("./csv/"+pyren_encode(csv_filename), "w", encoding='utf-8' )
csvf.write('\ufeff')
DTCpos = path.find('DTC')
if DTCpos > 0:
@ -480,12 +482,15 @@ class ECU:
if mod_globals.opt_csv_human and csvf!=0:
csvline = csvline + "\n"
csvline = csvline.replace(',','.')
csvline = csvline.replace(';',',')
csvf.write(pyren_decode(csvline).encode('utf8'))
csvline = csvline.replace('.',mod_globals.opt_csv_dec)
csvline = csvline.replace(',',mod_globals.opt_csv_dec)
csvline = csvline.replace(';',mod_globals.opt_csv_sep)
csvf.write(csvline)
csvf.flush()
time_delata = datetime.now().timestamp()-csv_start_time
csvline = datetime.fromtimestamp(time_delata).strftime("%S.%f")[:-3]
time_diff = int(round(time.time() * 1000))-csv_start_time
time_sec = str(time_diff//1000)
time_ms = str((time_diff)%1000)
csvline = time_sec.zfill(2)+mod_globals.opt_csv_dec+time_ms.zfill(3)
#Collect all the requests from the current screen
if mod_globals.opt_performance and self.elm.performanceModeLevel > 1 and mod_globals.opt_csv_only:
@ -664,11 +669,15 @@ class ECU:
self.elm.rsp_cache[req] = rsp
csvline = csvline + "\n"
csvline = csvline.replace(',','.')
csvline = csvline.replace(';',',')
csvf.write(pyren_decode(csvline).encode('utf8'))
csvline = csvline.replace('.',mod_globals.opt_csv_dec)
csvline = csvline.replace(',',mod_globals.opt_csv_dec)
csvline = csvline.replace(';',mod_globals.opt_csv_sep)
csvf.write(csvline)
csvf.flush()
csvline = datetime.fromtimestamp((reqTime-startTime)/1000).strftime("%S.%f")[:-3]
time_diff = reqTime-startTime
time_sec = str(time_diff//1000)
time_ms = str((time_diff)%1000)
csvline = time_sec.zfill(2)+mod_globals.opt_csv_dec+time_ms.zfill(3)
for dr in datarefs:
datastr = dr.name
@ -678,7 +687,7 @@ class ECU:
if dr.type=='Parameter':
datastr, help, csvd = get_parameter( self.Parameters[dr.name], self.Mnemonics, self.Services, self.elm, self.calc )
if csvf!=0 and (dr.type=='State' or dr.type=='Parameter'):
csvline += ";" + pyren_encode(csvd)
csvline += ';' + csvd
csvf.close()

View File

@ -17,6 +17,9 @@ opt_scan = False
opt_csv = False
opt_csv_only = False
opt_csv_human = False
opt_csv_sep = ","
opt_csv_dec = "."
opt_excel = False
opt_usrkey = ""
opt_verbose = False
opt_cmd = False
@ -38,7 +41,7 @@ opt_performance = False
opt_minordtc = False
opt_ref = "" #alternative ref set for acf
opt_mtc = "" #alternative mtc set for acf
dumpName = ""
dumpName = ""
state_scan = False

View File

@ -214,6 +214,12 @@ def optParser():
default=False,
action="store_true")
parser.add_argument("--excel",
help="Save csv in excel compatible format",
dest="excel",
default=False,
action="store_true")
options = parser.parse_args()
if not options.port and mod_globals.os != 'android':
@ -260,6 +266,13 @@ def optParser():
print("Development MODE")
mod_globals.opt_dev = True
mod_globals.opt_devses = options.dev
mod_globals.opt_excel = options.excel
if mod_globals.opt_excel:
mod_globals.opt_csv_sep = ';'
mod_globals.opt_csv_dec = ','
else:
mod_globals.opt_csv_sep = ','
mod_globals.opt_csv_dec = '.'
def main():
'''Main function'''