0

I have an existing csv file "meetingschedule.csv" and into its new column in 'Time' row , I have to add the current time in (%H:%M) format after the 4 columns of given times. But with my code , I am always getting the Time with commas in between:(

Like the Time --> 18:39 would be written as "1,8,:,3,9" as in the following meetingschedule.csv file -

Time,Meeting ID,Passcode
06:47, 999 9722 2425,
16:59, 999 9722 2425,
20:02, 999 9722 2425,
18:08, 999 9722 2425,
1,8,:,3,9

What should I do to fix this issue!? Also, If possible, also provide me the way to insert a comma and copy the Meeting ID from the above row and paste it as per its above lines, followed by a comma (As the Meeting ID also is variable here) after writing the Current_Time .

My Code is Given below--

import datetime , csv
from datetime import datetime

timestr = datetime.now().strftime("%H:%M")
with open("meetingschedule.csv","a") as csvFile:
    Fileout = csv.writer(csvFile)
    Fileout.writerow(timestr)

For example, I require the output as --

Time,Meeting ID,Passcode
06:47, 999 9722 2425,
16:59, 999 9722 2425,
20:02, 999 9722 2425,
18:08, 999 9722 2425,
18:39, 999 9722 2425,

Thanks For the Help !

PNxZEN
  • 51
  • 2
  • 6
  • 1
    `Fileout.writerow([timestr, '', ''])` –  Apr 07 '21 at 03:10
  • Thanks but its leaving an empty line before the timestr... Also, The Meeting ID also should be written to the csv....Thanksss bro – PNxZEN Apr 07 '21 at 03:42

2 Answers2

0

If I understand the question, you want to make a .csv file with multiple rows and add the timestamp in the first column? Probably the easiest way for you to do this is create a csv writer and specify the delimiter and just add the data with your lists. I named it new_writer below...

import csv
from datetime import datetime

timestr = datetime.now().strftime("%H:%M")

with open("meetingschedule.csv", "a") as csvFile:
    new_writer = csv.writer(csvFile, delimiter=',')
    new_writer.writerow(['Time', 'Meeting ID', 'Passcode'])

    # Data
    new_writer.writerow(['timestr', 'meeting IDs', 'passcodes'])

Edit: I'm not sure I understood the question actually. Mark Tolonen seems to have understood the problem: you need to separate the data using a list (which you can see formatted in my .writerows)

  • Thanks and It seems you're right, @Mark Tolonen has actually got it right , but your solution is helpful too if I have to create a new csv with given conditions, but since I have to work on an existing csv file...hence I would like to follow his answer.. :) – PNxZEN Apr 08 '21 at 04:55
0

csvwriter.writerow() takes a iterable (usually a list) with items to place in each column. Passing a string (an iterable of characters) ends up placing a single character in each column. To write the columns as described, you need a list of three items. To have a trailing comma, the last item should be an empty string.

Also make sure to open the file with newline='' as stated in the csv documentation. See csv-file-written-with-python-has-blank-lines-between-each-row for why.

import datetime , csv
from datetime import datetime

timestr = datetime.now().strftime("%H:%M")
with open("meetingschedule.csv","a",newline='') as csvFile:
    Fileout = csv.writer(csvFile)
    Fileout.writerow([timestr,'999 9722 2425',''])

Given your input file, and that you want to copy the previous line meeting value, you can read all the lines in, append a new one with the appropriate data, and write it back out:

import csv
from datetime import datetime

with open('meetingschedule.csv',newline='') as f:
    lines = list(csv.reader(f))
    
timestr = datetime.now().strftime("%H:%M")
addline = [timestr,lines[-1][1],''] # new information and previous ID
lines.append(addline)

with open('meetingschedule.csv','w',newline='') as f:
    writer = csv.writer(f)
    writer.writerows(lines)  # NOTE plural writerows takes a list of lists.
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Thank U Soo much broo...U did really understand my question ! ..I did not really expect to have such a clarified solution...Thanks again.. – PNxZEN Apr 08 '21 at 04:50