Take the 2-minute tour ×

I am trying to change the date format in a text file so that it can be comma-delimited by Year, Month, and Day. Currently, it is in YYYY-MM-DD format and I am trying to replace the hyphens with commas without affecting the other hyphenated text in the file:

2011-1-26,35,34,32,34,31,25,100,82,64,30.04,29.79,29.54,9,2,0,22,11,29,1.82,8,Fog-Rain-Snow-Thunderstorm,23,39.2,76.7,KBWI #I dont want to remove the hyphens for the weather events

A friend told me to use regex with this expression:


It does find the patterns for all the dates formatted with hyphens, but I have no clue as to how to replace the hyphens with commas for all the lines in the textfile with this match. I'm thinking about creating a new text file with the edited data so that I can use it in another application. Any help would be appreciated.

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

Without using regexp is simple:

in = open("inputfile", "r")
out = open("outputfile", "w")

for line in in:
    line = line.split(",", 1)
    line[0] = line[0].replace("-", ",")

share|improve this answer
Thank you so much. This one helped out alot. At this point in this course, we're doing string manipulation and I couldn't figure a way to join the rest of the string with the edited parts. –  user1022909 Nov 1 '11 at 2:05
add comment

Using fileinput is a convenient way to alter files "in-place":

import fileinput
import re
import sys

for line in fileinput.input(filename,inplace=True):

re.sub was used to replace the hyphens with commas. The first regex pattern matches only the dates, leaving the other hyphens in the lines untouched. The second argument r'\1,\2,\3' tells re.sub to replace the matched text with the first matched group \d{4}, followed by a comma, then the second matched group, \d{1,2}, followed by another comma, followed by the third matched group, \d{1,2}.

share|improve this answer
I'll try this method out as well for a future project I have to do involving input and output. I'm unsure of what to do involving regular expressions and whatnot. Beginner at Python. –  user1022909 Nov 1 '11 at 2:06
add comment

The square brackets aren't necessary around the \d entries.

Try using re.sub with group numbers for the replace operation:

>>> d = '2011-1-1,59,44,29,55,42,26,93,80,66,30.15,30'
>>> re.sub(r'(\d{4})-(\d{1,2})-(\d{1,2})', r'\1,\2,\3', d)
share|improve this answer
add comment

Your Answer


