我的代码不是很好,但这是一个非常有趣的问题。在字符串中查找正斜杠时,除了正斜杠在文件的最后一个单词中之外,所有的都可以找到。这是我的代码。
#!/usr/bin/python
import sys
if len(sys.argv)!=2:
print "usage: %s filename\n" % (sys.argv[0]);
exit(0);
f = open(sys.argv[1]);
lines = [i for i in f.readlines()]
finals = [];
for line in lines:
words = line.split(",");
for word in words:
if word.find("/") != -1:
datefixes = word.split("/")
if datefixes[2].__len__() == 4:
temp = datefixes[2]
word = datefixes[0] + "-" + datefixes[1] + "-" + temp[-2:]
finals += "," + word;
tempstring = ''.join(finals)
finallist = tempstring.split("\r\n")
finalstring = ""
for tmpstrpart in finallist:
if tmpstrpart != "" or tmpstrpart !="\r\n":
finalstring += tmpstrpart[1:] + "\r\n"
print finalstring
这是一个示例输入
ACPVBF,1930-729,Z729,12/16/2014,6/10/2008,1/5/2003,44-48-46,39-43-41,35-39-37,29-33-31
ACPVGT,1930-729,Z729,25-29-27,19-23-21,14-18-16,7/11/2009,2/6/2004,48-2-0,42-46-44
ACPUQH,1930-729,Z729,32-40-19,26-34-13,21-29-8,14-22-1,9/17/1946,5/13/1942,49-7-36
ACPVOU,1930-729,Z729,42-0-29,36-44-23,31-39-18,24-32-11,19-27-6,15-23-2,9/17/1946
在代码中,这些行用逗号分隔。如果末尾的单词包含 /,则找不到正斜杠。但前提是它在最后。其余的工作正常。
编辑:我目前在这些线上得到的输出是:
ACPVBF,1930-729,Z729,12-16-14,6-10-08,1-5-03,44-48-46,39-43-41,35-39-37,29-33-31
ACPVGT,1930-729,Z729,25-29-27,19-23-21,14-18-16,7-11-09,2-6-04,48-2-0,42-46-44
ACPUQH,1930-729,Z729,32-40-19,26-34-13,21-29-8,14-22-1,9-17-46,5-13-42,49-7-36
ACPVOU,1930-729,Z729,42-0-29,36-44-23,31-39-18,24-32-11,19-27-6,15-23-2,9/17/1946
我试图从这些行中获得的输出是:
ACPVBF,1930-729,Z729,12-16-14,6-10-08,1-5-03,44-48-46,39-43-41,35-39-37,29-33-31
ACPVGT,1930-729,Z729,25-29-27,19-23-21,14-18-16,7-11-09,2-6-04,48-2-0,42-46-44
ACPUQH,1930-729,Z729,32-40-19,26-34-13,21-29-8,14-22-1,9-17-46,5-13-42,49-7-36
ACPVOU,1930-729,Z729,42-0-29,36-44-23,31-39-18,24-32-11,19-27-6,15-23-2,[9-17-46]
我希望它周围有括号的那个也可以改变。
基于 BrenBarn 的回答的最终工作代码:
#!/usr/bin/python
import sys
import re
if len(sys.argv)!=2:
print "usage: %s filename\n" % (sys.argv[0]);
exit(0);
f = open(sys.argv[1]);
x = f.read()
f.close()
filename = sys.argv[1]
filename = filename[:-4] + " finished.csv"
f = open(filename, 'w')
f.write(re.sub(r'(\d{1,2})/(\d{1,2})/\d{2}(\d{2})', r'\1-\2-\3', x))
f.close()
感谢所有的帮助。抱歉,我还不能投票。