2

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-1,59,44,29,55,42,26,93,80,66,30.15,30.05,29.96,10,8,4,14,3,22,T,7,Rain,201,39.2,76.7,KBWI
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:

re.findall('[\d]{4}-[\d]{1,2}-[\d]{1,2}')

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.

3 Answers 3

2

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):
    sys.stdout.write(re.sub(r'(\d{4})-(\d{1,2})-(\d{1,2})',r'\1,\2,\3',line))

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}.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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("-", ",")
    out.write(','.join(line))

in.close()
out.close()

1 Comment

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.
1

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)
'2011,1,1,59,44,29,55,42,26,93,80,66,30.15,30'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.