Transpose CSV files in Python Part 1

Easy CSV transposition I wondered how many lines it would take

csvt.py


# -*- coding: utf-8 -*-
import sys
import numpy
fin = open(sys.argv[1], "r")
fout = open(sys.argv[2], "w")
for line in numpy.array([s.strip('\n').split(',') for s in fin]).T:
    fout.write(",".join(line) + "\n")
fin.close()
fout.close()

Run


python csvt.py in.csv out.csv

in.csv


1,2,3
4,5,6
7,8,9

out.csv


1,4,7
2,5,8
3,6,9

Continue.

Recommended Posts

Transpose CSV files in Python Part 1
Transpose CSV file in Python Part 2: Performance measurement
Csv in python
[Python] Reading CSV files
Reading and writing CSV and JSON files in Python
UI Automation Part 2 in Python
[Python] Loading csv files using pandas
Manipulate files and folders in Python
Handling of JSON files in Python
Download Google Drive files in Python
[R] [Python] Memo to read multiple csv files in multiple zip files
Sort large text files in Python
Handle Excel CSV files with Python
Read files in parallel with Python
Export and output files in Python
Extract strings from files in Python
Basic Linear Algebra Learned in Python (Part 1)
Data input / output in Python (CSV, JSON)
GUI creation in python using tkinter part 1
AM modulation and demodulation in Python Part 2
Find files like find on linux in Python
Output tree structure of files in Python
Type annotations for Python2 in stub files!
Draw a heart in Python Part 2 (SymPy)
Read all csv files in the folder
Referencing INI files in Python or Ruby
Automate jobs by manipulating files in Python
Read and write JSON files in Python
Sample for handling eml files in Python
[Part1] Scraping with Python → Organize to csv!
Download files in any format using Python
How to read CSV files in Pandas
Quadtree in Python --2
QGIS + Python Part 2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
QGIS + Python Part 1
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
Python: Scraping Part 1
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Disassemble in Python