1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#! /usr/bin/python
# -*- coding: utf-8 -*-
__author__ = 'Jason'
# Create: 20160114
# Modified on: 20160115
# Simple python script that convert csv to tab (-c or --csv option) or convert tab to csv (-t or --tab option).
# Support stdin and stdout
from optparse import OptionParser
import sys
def getOptions():
parser = OptionParser(usage="%prog [-i INPUT] [-csv'-tab] [-o OUT]", version="%prog 1.0")
parser.add_option("-i", "--input", type="string", dest="input", action="store", help="Input file. File should be seperated by tab or comma. If not specify, use stdin", metavar="file")
parser.add_option("-o", "--output", type="string", dest="output", action="store", help="Output file path. If not specify, use stdout.", metavar="file")
parser.add_option("-t", "--tab", default=False, dest="tab", action="store_true", help="Line field is tab splitted" )
parser.add_option("-c", "--csv", default=True, dest="csv", action="store_true", help="Line field is ',' splitted. Default True.")
(options, args) = parser.parse_args()
return options
def readLine(path):
"""
Yeild a line for processing.
:param path: Input file path. If path not specified, use stdin instead.
:return: line string
"""
if path:
for line in open(path):
yield line
else:
for line in sys.stdin.readlines():
yield line
def csv2tab(line):
"""
Just simplely convert csv to tab. Do not concert csv format with complex condition
:param line:
:return:
"""
line=line.replace(',"','"').replace('",','"')
t=line.split('"')#if , is enclosed by " , then , in odd number
newt=[]
if len(t) <=2 :
return line.replace(',','t')
else:
for i,e in enumerate(t):
if i%2==1:
newt.append(e)
else:
if e=='': continue
newt.extend(e.split(','))
return 't'.join(newt)
def tab2csv(line):
temp=[]
for t in line.split('t'):
if ',' in t:
temp.append('"%s"' % t) # if comma in field, use double quotation marks to enclose comma
else:
temp.append(t)
return ','.join(temp)
options=getOptions()
if options.output:
out=open(options.output,'w')
else: out=sys.stdout # if output path not specified, use stdout instead
for line in readLine(options.input):
if options.tab:
newLine=tab2csv(line)
else:
newLine=csv2tab(line)
out.write(newLine)
out.flush()
out.close()
|