1

Quick question on how to properly write data back into a CSV file using the python csv module. Currently i'm importing a file, pulling a column of dates and making a column of days_of_the_week using the datetime module. I want to then write out a new csv file (or overright the individual one) containing one original element and the new element.

with open('new_dates.csv') as csvfile2:
    readCSV2 = csv.reader(csvfile2, delimiter=',')
    incoming = []
    for row in readCSV2:
         readin = row[0]
         time = row[1]
         year, month, day = (int(x) for x in readin.split('-'))
         ans = datetime.date(year, month, day)
         wkday = ans.strftime("%A")
         incoming.append(wkday)
         incoming.append(time)
with open('new_dates2.csv', 'w') as out_file:
    out_file.write('\n'.join(incoming))

Input files looks like this:

2017-03-02,09:25
2017-03-01,06:45
2017-02-28,23:49
2017-02-28,19:34

When using this code I end up with an output file that looks like this:

Friday
15:23
Friday
14:41
Friday
13:54
Friday
7:13 

What I need is an output file that looks like this:

Friday,15:23
Friday,14:41
Friday,13:54
Friday,7:13 

If I change the delimiter in out_file.write to a comma I just get one element of data per column, like this:

Friday  15:23   Friday 14:41  Friday 13:54  ....

Any thoughts would be appreciated. Thanks!

6
  • 1
    The problem might be this join operation: '\n'.join(incoming). Try replacing that \n with a space to get the desired output; for a real csv, use "," Commented Apr 26, 2017 at 15:21
  • using a space results all data in one long row, instead of two CSV columns. And as noted the use of a comma results in each value in a different field in a single row. Commented Apr 26, 2017 at 15:25
  • What does the input file look like? Commented Apr 26, 2017 at 15:31
  • print wkday print time Thursday 16:45 Wednesday 20:42 Monday 12:12 Saturday 07:08 Commented Apr 26, 2017 at 15:37
  • Please edit the question to show the input file that creates the output. Make it reproducible. Commented Apr 26, 2017 at 15:45

4 Answers 4

1

Being somewhat unclear on what format you want, I've assumed you just want a single space between wkday and time. For a quick fix, instead of appending both wkday and time separately, as in your example, append them together:

...
    incoming.append('{} {}'.format(wkday,time))
...

OR, build your incoming as a list of lists:

...
    incoming.append([wkday,time])
...

and change your write to:

with open('new_dates2.csv', 'w') as out_file:
    out_file.write('\n'.join([' '.join(t) for t in incoming]))
Sign up to request clarification or add additional context in comments.

4 Comments

Using that method seems to yield the same results as replace the \n with a space. All the data goes into a single field.
The second option makes some even stranger results. All data is in a single column and each field is one character.
@Justin sorry, missed a chunk out - above should work now
Traceback (most recent call last): File "./analyze.py", line 32, in <module> out_file.write('\n'.join(' '.join(incoming))) TypeError: sequence item 0: expected string, list found
0

It seems you want Friday in column 0 and the time in column 1, so you need to change your incoming to a list of lists. That means the append statement should look like this:

...
    incoming.append([wkday, time])
...

Then, it is better to use the csv.writer to write back to the file. You can write the whole incoming in one go without worrying about formatting.

with open('new_dates2.csv', 'w') as out_file:
    writer = csv.writer(out_file)
    writer.writerows(incoming)

2 Comments

Results in an error: Traceback (most recent call last): File "./analyze.py", line 35, in <module> out_file.write('\n'.join(incoming)) TypeError: sequence item 0: expected string, list found
@Justin Check the answer as it currently stands - I edited it.
0

Basically your incoming array is a linear list. So, you should have been doing is something like following:

#your incoming array
incoming = ['Friday', '15:23', 'Friday', '14:41', 'Friday', '13:54', 'Friday', '7:13']

#actual parsing of the array for correct output
for i,j in zip(incoming[::2], incoming[1::2]):
        out_file.write(','.join((i,j)))
        out_file.write('\n')

Comments

0

You don't really need the csv module for this. I'm guessing at the input, but from the description it looks like:

2017-03-02,09:25
2017-03-01,06:45
2017-02-28,23:49
2017-02-28,19:34

This will parse it and write it in a new format:

import datetime
with open('new_dates.csv') as f1, open('new_dates2.csv','w') as f2:
    for line in f1:
        dt = datetime.datetime.strptime(line.strip(),'%Y-%m-%d,%H:%M')
        f2.write(dt.strftime('%A,%H:%M\n'))

Output file:

Thursday,09:25
Wednesday,06:45
Tuesday,23:49
Tuesday,19:34

2 Comments

This is assuming there's nothing else in the input file. OP seems to want CSV output too.
@Stuart The question changed since I wrote this, but still, adding a comma is a slight modification to the strftime. No assuming involved, since the OP also added the input.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.