Csv in python

Read data from csv file in Python

(python3)

import csv
csvfile = open("filename", 'rt')
contents = csv.reader(csvfile, delimeter=',')
    #Read the value of delimiter as a delimiter.
for row in contents:
    """
Even if contents are output as they are, the contents of the file will be
Since it is not output, each line is fetched.
    """
    print(row)

For example

The contents of filename

,A,B,C,D,E
1,234,513,25345,5243,52
2,513,52345,523452,5234,45
3,31,5432,54,63,63

If so, If you write the code as below,

import csv
import numpy as np
csvfile = open("filename", 'rt')
contents = csv.reader(csvfile, delimiter=',')
array = np.array( [ row for row in contents ] )
array = array[1:, 1:]
print(array)

The following output result is obtained.

[['234' '513' '25345' '5243' '52']
 ['513' '52345' '523452' '5234' '45']
 ['31' '5432' '54' '63' '63']]

Recommended Posts

Csv in python
Transpose CSV files in Python Part 1
Quadtree in Python --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
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Data input / output in Python (CSV, JSON)
Sorted list in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python
Logistic distribution in Python
LU decomposition in Python
One liner in Python