python - How would you put back a list that you've split and transformed back to the original file? -


i trying isolate date date/time column in original csv file. i've isolated column , change date having tough time putting original file:

def isolatedate(csv_file):     file_in = open(csv_file, "ru")     reader = csv.reader(file_in)     next(file_in,none)     line in reader:         date = line[3]         date = date.split()         new_date = date[0]     return new_date 

how go putting new_date variable line[3] in original file

you need read in rows file, modify 4th column (index 3) , rewrite file:

with open(csv_file, "r") file_in:     reader = csv.reader(file_in)     header = next(reader)     rows = [row[0:3] + [row[3].split()[0]] + row[4:]  row in reader]     #                  ^^^modified date^^^  open(csv_file, "w") file_in:     writer = csv.writer(file_in)     writer.writerow(header)     writer.writerows(rows) 

Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -