-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtxt2tsv.py
executable file
·41 lines (34 loc) · 1.04 KB
/
txt2tsv.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import re
def readTxt(filename):
with open(filename, "r") as f:
return f.readlines()
def convert(lines):
convertedLines = []
i = 0
for line in lines:
if re.match("\s+", line):
continue
words = line.split()
newLine = []
for j in range(len(words)):
newLine.extend(str(i+1) + "-" + str(j+1) + "\t" + words[j] + "\n")
convertedLines.append(newLine)
i+=1
return convertedLines
def writeTsv(lines, filename):
with open(filename, "w") as f:
for line in lines:
f.write("".join(line) + "\n")
def convertFile(filenameIn, filenameOut):
writeTsv(convert(readTxt(filenameIn)), filenameOut)
def convertFiles(pathIn, pathOut):
for filename in os.listdir(path):
convertFile(os.path.join(path, filename), os.path.join(pathOut, filename.replace(".txt", ".tsv")))
if __name__=="__main__":
path = sys.argv[1]
pathOut = sys.argv[2]
convertFiles(path, pathOut)