[PYTHON] Handle CSV that contains the element you want to parse in the file name

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

Handle CSV that contains the element you want to parse in the file name
I want to get the file name, line number, and function name in Python 3.4
I want to see the file name from DataLoader
[Python] When you want to use all variables in another file
Normalize the file that converted Excel to csv as it is.
If you want to assign csv export to a variable in python
I want to make the second line the column name in pandas
Format the Git log and get the committed file name in csv format
A note that you want to manually decorate the parameters passed in the Django template form item by item
I want to handle the rhyme part1
Memorandum Regular expression When there are multiple characters in the character string that you want to separate
I want to handle the rhyme part3
The one that divides the csv file, reads it, and processes it in parallel
The file name was bad in Python and I was addicted to import
I want to handle the rhyme part2
I want to handle the rhyme part5
I want to handle the rhyme part4
Various ways to read the last line of a csv file in Python
How to get the notebook name you are currently using in Google Colab
Use PIL in Python to extract only the data you want from Exif
Solution to the problem that you can't activate by putting conda in pyenv
Extract only the file name excluding the directory in the directory
When you want to update the chrome driver.
I want to handle the rhyme part7 (BOW)
I want to display the progress in Python!
Do you want me to fix that copy?
It's okay to participate for the first time! A hackathon starter kit that you want to prepare "before" participating in the hackathon!
I want to replace the variables in the python template file and mass-produce it in another file.
How to find the first element that matches your criteria in a Python list
Parse a JSON string written to a file in Python
How to get the variable name itself in python
Specify the file name when sending the csv attached mail
I tried to touch the CSV file with Python
Read the csv file and display it in the browser
I want to randomly sample a file in Python
Python program that looks for the same file name
I want to write in Python! (3) Utilize the mock
I want to handle the rhyme part6 (organize once)
When you want to plt.save in a for statement
The programming language you want to be able to use
I want to use the R dataset in python
I want to handle the rhyme part8 (finished once)
Make a note of what you want to do in the future with Raspberry Pi
[Python3] Code that can be used when you want to resize images in folder units
[Python] When you want to import and use your own package in the upper directory
Golang: The matter that you want to execute the process at exactly 00:00 or 30 minutes at the specified interval.
If you want to put an argument in the closure function and execute it later
[Python] Open the csv file in the folder specified by pandas
[python] Change the image file name to a serial number
Change the standard output destination to a file in Python
How to import a file anywhere you like in Python
Get the file name saved in AWS S3 (1000 or more)
Batch convert all xlsx files in the folder to CSV files
Try to extract the keywords that are popular in COTOHA
In omegaconf, let's pass the direct parameter file to the function
If you want your colleagues to use the same language
I want to make the Dictionary type in the List unique
I want to align the significant figures in the Numpy array
Try to decipher the garbled attachment file name with Python
Command to list all files in order of file name
I didn't want to write the AWS key in the program