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

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -