CSV is a very convenient file format when using EXCEL. Language like R
has very powerful functions reading and writing CSV files. In Python,
csv
module provides read and write capability of tabular data in CSV format.
Suppose a file named "tp.csv" with following content:
Gene observed1 expected1 observed2 expected2 TTN 27 33 60 54 GATA3 38 20 17 35 HLA-DRB6 18 15 24 27 MUC16 13 15 28 26 NR1H2 11 15 29 25 GPRIN2 12 14 27 25 MAP3K1 15 14 24 25 GPRIN1 13 14 25 24 MLL3 12 14 26 24 MAP3K4 8 14 29 23 CDH1 17 12 17 22 ENSG00000245549 15 12 18 21 ZNF384 12 12 20 20 FRG1B 11 11 20 20 AKD1 9 11 21 19 OBSCN 12 11 17 18 NCOA3 8 10 20 18 USH2A 8 10 20 18 ENSG00000198786 12 10 15 17Python script file "test.py" to read the file:
#!C:\python\python.exe import csvwith open("tp.csv","r")as rad: lines = csv.reader(rad, delimiter='\t')for rowin lines:
C:\> test.py
Gene -- observed1 -- expected1 -- observed2 -- expected2 TTN -- 27 -- 33 -- 60 -- 54 GATA3 -- 38 -- 20 -- 17 -- 35 HLA-DRB6 -- 18 -- 15 -- 24 -- 27 MUC16 -- 13 -- 15 -- 28 -- 26 NR1H2 -- 11 -- 15 -- 29 -- 25 GPRIN2 -- 12 -- 14 -- 27 -- 25 MAP3K1 -- 15 -- 14 -- 24 -- 25 GPRIN1 -- 13 -- 14 -- 25 -- 24 MLL3 -- 12 -- 14 -- 26 -- 24 MAP3K4 -- 8 -- 14 -- 29 -- 23 CDH1 -- 17 -- 12 -- 17 -- 22 ENSG00000245549 -- 15 -- 12 -- 18 -- 21 ZNF384 -- 12 -- 12 -- 20 -- 20 FRG1B -- 11 -- 11 -- 20 -- 20 AKD1 -- 9 -- 11 -- 21 -- 19 OBSCN -- 12 -- 11 -- 17 -- 18 NCOA3 -- 8 -- 10 -- 20 -- 18 USH2A -- 8 -- 10 -- 20 -- 18 ENSG00000198786 -- 12 -- 10 -- 15 -- 17
csv.writer
object write data into a CSV file:
#!C:\python\python.exe import csvwith open("tp.csv","w")as writfile: writ = csv.writer(writfile, delimiter=',') writ.writerow([1,2,3,4,5]) writ.writerow("python tutorial")
C:\> test.pyThe content of file "tp.csv":
1,2,3,4,5 p,y,t,h,o,n, ,t,u,t,o,r,i,a,l