[Python] Convert csv file delimiters to tab delimiters

Thing you want to do

--I want to convert the delimiter of the csv file to tab delimiter. --If the file contains commas such as addresses, it is easier to perform subsequent processing (for example, importing into DB) by separating them into tabs.

point

~~-Easy to import and use csv module ~~ ~~ Import the csv module in the calling program ~~

--Import csv module at the beginning of def file --Read and write the target file with open --Read the target files separated by commas and export them separated by tabs → Overwritten on the target file --After reading using csv.reader, store it in an array row by row --For writing, write the array read in ↑ with `writer.writer rows (array name)` at once. If you want to write line by line, you can write with `writer.writerrow (matrix)`

convertTav.py



def CsvToTsv(path):
    
    #Array for reading
    line = []

    #Read
    with open(path, "r", newline="") as f:
            #Create read object (comma separated)
            reader = csv.reader(f, delimiter = ",")
            #Read
            line = [row for row in reader]
    #close
    f.close

    #writing
    with open(path, "w", newline="") as f:
            #Create write object (tab delimited)
            writer = csv.writer(f, delimiter = "\t")
            #Write all together
            writer.writerows(line)
    #close
    f.close

Postscript

If you want to convert encode by reading and writing, specify encoding at open (Example) Shift-jis when reading, utf-8 when writing

convertTav.py


#Read
with open(path, "r", newline="", encoding="cp932") as f:

#writing
with open(path, "w", newline="", encoding="utf_8") as f:

** It didn't work with Shift-jis, it worked with cp932 **

Recommended Posts

[Python] Convert csv file delimiters to tab delimiters
[Python] How to convert db file to csv
How to convert JSON file to CSV file with Python Pandas
[Python] Write to csv file with Python
Output to csv file with Python
How to convert Python to an exe file
Convert psd file to png in Python
[Python] Convert CSV file uploaded to S3 to JSON file with AWS Lambda
After calling the Shell file on Python, convert CSV to Parquet.
Read Python csv file
How to read a CSV file with Python 2/3
[Python] Convert from DICOM to PNG or CSV
Convert svg file to png / ico with Python
Writing logs to CSV file (Python, C language)
Read CSV file with Python and convert it to DataFrame as it is
[python] Convert date to string
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
Convert HTML to text file
I tried to touch the CSV file with Python
[Python] Convert PDF text to CSV page by page (2/24 postscript)
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Write to csv with Python
Convert SDF to CSV quickly
How to convert Json file to CSV format or EXCEL format
Python script to create a JSON file from a CSV file
Convert genbank file to gff file
Download csv file with python
Convert python 3.x code to python 2.x
Convert Excel file to text in Python for diff purposes
Workflow to convert formula (image) to python
Convert list to DataFrame with python
Python> list> Convert double list to single list
Python CSV file reading and writing
[Python] Convert natural numbers to ordinal numbers
Convert decimal numbers to n-ary numbers [python]
Python> tuple> Convert double tuple to single tuple
File upload to Azure Storage (Python)
I tried to convert a Python file to EXE (Recursion error supported)
[Python] How to store a csv file as one-dimensional array data
[Good By Excel] python script to generate sql to convert csv to table
Convert memo at once with Python 2to3
Convert Python> two value sequence to dictionary
Read CSV file with python (Download & parse CSV file)
How to convert csv to tsv in CLI
Convert matplotlib graphs to emf file format
Convert from PDF to CSV with pdfplumber
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Convert from katakana to vowel kana [python]
Convert FX 1-minute data to 5-minute data with Python
python> Convert tuple to list> aList = list (pi_tuple)
Read Python csv and export to txt
Convert Python date types to RFC822 format
[Part1] Scraping with Python → Organize to csv!
Python> Output numbers from 1 to 100, 501 to 600> For csv
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
Convert from Markdown to HTML in Python
Convert absolute URLs to relative URLs in Python