For example, a file like logdata_20150725.csv.
There is nothing wrong if the CSV file contains a date data column, but if only the file name has a date, it is awkward to handle. I'll use Python to get rid of this challenge.
An environment where Python can be used. It is assumed that the elements you want to retrieve are separated by an underscore (_).
Since it is split by'split' on the 11th line, other split characters can be handled by operating here.
import csv
import os
FileList = os.listdir()
for FileName in FileList:
ReadFile = open(FileName, 'r')
WriteFile = open(FileName.split('_')[1]+'.csv', 'w')
reader = csv.reader(ReadFile)
header = next(reader)
writer = csv.writer(WriteFile, lineterminator='\n')
for row in reader:
addrow = [FileName.split('_')[1]]
addrow.extend(row)
writer.writerow(addrow)
ReadFile.close()
WriteFile.close()
Since it is a disposable script, please execute it in the folder where the file is stored. Read the files one by one and save them with a different name (in this case, date.csv).
--Use the os module to store the file list in FileList --Store the file you want to read in ReadFile --Give WriteFile the name of the file you want to write --Split the FileName element of the for statement and use the second element as the file name (FileName.split ('_') [1] part) --Create reader, header, writer using csv module --Store date element on line --Add the element originally stored in the CSV file to the right of the line (extend) --Write to CSV file using writer
It is a flow.
reference http://qiita.com/okadate/items/c36f4eb9506b358fb608
Recommended Posts