나는 엑셀 스프레드 시트를 가지고있다. 나는 Access로 옮길 준비를하고 있고, 날짜 컬럼은 1963 년부터 1969 년까지, 1968 년 8 월에서 1968 년 9 월까지, 1972 년 3 월에서 73 년 24 -Jul, 1980 년 10 월 2 일, 1980 년 8 월 29 일, 1946 년 7 월 등. 나는 csv에 키 (지도 번호)와 날짜 열이 될 열을 가져 와서 csv에 다시 쓰고 있습니다. 범위는 아니지만 4 자리 숫자를 삭제할 수 있습니다. 그리고 나는 손으로 다시 포맷하기에 부족한 날과 2 자리 연도를 추출하는 방법을 잘 모른다. 내 코드는 매우 우아하고 아마 가장 좋은 방법이 아닙니다 :날짜로 카테고리
import csv, xlwt, re
# create new Excel document and add sheet
# from tempfile import TemporaryFile
from xlwt import Workbook
book = Workbook()
sheet1 = book.add_sheet('Sheet 1')
# populate first row with header
sheet1.write(0,0,"Year")
sheet1.write(0,1,"Map")
sheet1.write(0,2,"As Entered")
# count variable for populating sheet
rowCount=0
# open csv file and read
with open('C:\dateTestMSDOs.csv', 'rb') as f:
reader=csv.reader(f)
for row in reader:
map = row[0] # first row is map number
dateRaw = row[1] # second row is raw date as entered
# write undated and blank entries
if dateRaw == 'undated':
yearStr = '0000'
rowCount +=1
sheet1.write(rowCount, 0, yearStr)
sheet1.write(rowCount, 1, map)
sheet1.write(rowCount, 2, dateRaw)
#print rowCount, yearStr, map, dateRaw, '\n'
yearStr=''
if dateRaw == '':
yearStr = 'NoEntry'
rowCount +=1
sheet1.write(rowCount, 0, yearStr)
sheet1.write(rowCount, 1, map)
sheet1.write(rowCount, 2, dateRaw)
#print rowCount, yearStr, map, dateRaw, '\n'
yearStr=''
# search and write instances of four consecutive digits
try:
year = re.search(r'\d\d\d\d', dateRaw)
yearStr= year.group()
#print yearStr, map, dateRaw
rowCount +=1
sheet1.write(rowCount, 0, yearStr)
sheet1.write(rowCount, 1, map)
sheet1.write(rowCount, 2, dateRaw)
#print rowCount, yearStr, map, dateRaw, '\n'
yearStr=''
# if none exist flag for cleaning spreadsheet and print
except:
#print 'Nope', map, dateRaw
rowCount +=1
yearStr='Format'
sheet1.write(rowCount, 0, yearStr)
sheet1.write(rowCount, 1, map)
sheet1.write(rowCount, 2, dateRaw)
#print rowCount, yearStr, map, dateRaw, '\n'
yearStr=''
yearStr=''
dateRaw=''
book.save('D:\dateProperty.xls')
print "Done!"
내가 추가 열으로 월과 일 쓰기뿐만 아니라 다양한 항목의 두 번째 4 자리 날짜를 당겨 싶습니다.
[datetime] (https://docs.python.org/2/library/datetime.html)의 날짜 형식을 살펴 보셨습니까? – N1B4
[Pandas] (http://pandas.pydata.org)에는 강력한 날짜 분석 기능이 있습니다. 나는 그것을 살펴볼 것이다. – b10n