
Google BigQuery에 데이터를 업로드할 수 있도록 공급업체로부터 받은 이 csv 파일의 날짜 형식을 변환하려고 합니다. Google Cloud Console의 VM을 사용하고 있습니다.
데이터는 다음과 같습니다.
Name ,Phone ,SalesDate ,Venue ,NoOfUnits ,ModifiedDatae
Victor ,5555555 ,12/6/2013 10:26:32 AM , Colosseum ,1 ,12/8/2013 1:05:45 PM
다음과 같은 형식으로 만들려고 합니다.
Name ,Phone ,SalesDate ,Venue ,NoOfUnits ,ModifiedDatae
Victor ,5555555 ,2013-12-6 10:26:32 ,Colosseum,1 ,2013-12-8 13:05:45
sed나 awk를 사용할 수 있다는 것을 알고 있습니다.
답변1
나는 여러분이 원하는 것을 수행하는 Python 스크립트와 Bash 스크립트를 작성했습니다.
파이썬 솔루션
다음은 질문에 지정된 대로 모든 시간 필드를 한 형식에서 다른 형식으로 변환하는 Python 스크립트입니다.
#!/usr/bin/env python3
# -*- coding: ascii -*-
"""reformat_time.py
Change date format from:
MM/DD/YYYY HH:MM:SS am/pm
to:
YYYY-MM-DD HH:MM:SS
in a CSV file
"""
import csv
from datetime import date
from datetime import datetime
import sys
# Open the file (taken as a command-line argument)
with open(sys.argv[1], 'r') as csvfile:
# Parse the CSV data
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
# Iterate over the rows
for row in csvreader:
# Iterate over the columns of each row
for index, col in enumerate(row):
# Try to parse and convert each column
try:
_datetime = datetime.strptime(col, "%m/%d/%Y %H:%M:%S %p")
newcol = _datetime.strftime("%Y-%m-%d %H:%M:%S")
# If parsing fails, leave the column unchanged
except ValueError:
newcol = col
# Update the column value
row[index] = newcol
# Output the updated row
print(','.join(row))
CSV 파일이 호출되고 data.csv
다음 줄(게시물에서 가져온)을 포함한다고 가정합니다.
Victor,5555555,12/6/2013 10:26:32 AM,Colosseum,1,12/8/2013 1:05:45 PM
그런 다음 다음과 같이 스크립트를 실행할 수 있습니다.
python reformat_time.py data.csv
그러면 다음과 같은 출력이 생성됩니다.
Victor,5555555,2013-12-06 10:26:32,Colosseum,1,2013-12-08 01:05:45
배쉬 솔루션
date
다음은 (거의) 동일한 효과를 갖는 GNU 유틸리티를 사용하는 Bash 스크립트입니다 .
#!/bin/bash
# reformat_time.sh
# Loop over the lines of the file
while read -r line; do
# Extract the field values for each row
Name="$(echo ${line} | cut -d, -f1)";
Phone="$(echo ${line} | cut -d, -f2)";
SalesDate="$(echo ${line} | cut -d, -f3)";
Venue="$(echo ${line} | cut -d, -f4)";
NoOfUnits="$(echo ${line} | cut -d, -f5)";
ModifiedDate="$(echo ${line} | cut -d, -f6)";
# Convert the time-fields from the old format to the new format
NewSalesDate="$(date -d "${SalesDate}" "+%Y-%m-%d %H:%M:%S")";
NewModifiedDate="$(date -d "${ModifiedDate}" "+%Y-%m-%d %H:%M:%S")";
# Output the updated row
echo "${Name},${Phone},${NewSalesDate},${Venue},${NoOfUnits},${NewModifiedDate}";
done < "$1"
다음과 같이 실행할 수 있습니다.
bash reformat_time.sh data.csv
그리고 다음과 같은 출력이 생성됩니다.
Victor ,5555555 ,2013-12-06 10:26:32, Colosseum ,1 ,2013-12-08 13:05:45
Bash 스크립트는 훨씬 더 취약합니다. 오류 처리는 수행되지 않으며 3번째와 6번째 필드에만 영향을 미칩니다. 또한 위의 Python 스크립트에서는 유지되지 않는 필드 구분 기호 주변의 공백도 유지합니다.
답변2
저는 Linux를 처음 접했고 날짜 형식을 변환하려고 합니다.
date
스위치를 사용해 보십시오 -d
:
-d, --date=STRING '지금'이 아닌 STRING으로 설명되는 표시 시간
그런 다음 원하는 방식으로 출력 형식을 지정하십시오.
예:
date -d "12/6/2013 10:26:32 AM" "+%F %H:%M:%S"
2013-12-06 10:26:32
형식화에 대한 설명은 man date
( FORMAT
섹션)을 참조하세요.
답변3
이 awk로 시도해 볼 수 있습니다
awk -F, '
function cvtdate( dat, array) {
split(dat,array,"/| |:")
array[4]=array[7]=="PM"?(array[4]+12):array[4]
return array[3]"-"array[1]"-"array[2]" "array[4]":"array[5]":"array[6]
}
{
$3=cvtdate($3)
$6=cvtdate($6)
}1' OFS=',' infile
답변4
또 다른 가능한 awk oneliner:
awk -F, '{ a[3];a[6] ; for (i in a) "date -d \""$i"\" \"+%Y-%m-%d %H:%M:%S\"" |& getline $i }1' OFS=, filename