为什么不用re来做同样的事情呢?
import re
str = "'`col` array<struct< columnname:string,columnname1:int,columnname2:decimal(10,0),columnname3:decimal(9,2)>>'"
word = re.findall("`\w+`",str,)
word = " ".join(word)
word = re.sub(r'\W+', '', word)
columnnames = re.findall(r"(\w+):",str)
for c in columnnames:
c = re.sub(r'\W+', '', c)
print "%s.%s," %( word,c)
输出:
col.columnname,
col.columnname1,
col.columnname2,
col.columnname3,
从您可以使用的文件中读取
open(filename,mode)
方法
import re
with open("test.txt","r") as h:
str = h.read()
word = re.findall(r"`\w+`",str,)
word = " ".join(word)
word = re.sub(r'\W+', '', word)
columnnames = re.findall(r"(\w+):",str)
for c in columnnames:
c = re.sub(r'\W+', '', c)
print "%s.%s," %( word,c)
要写入文件:
import re
with open("test.txt","r") as h:
with open("output.dat","a") as w:
str = h.read()
word = re.findall(r"`\w+`",str,)
word = " ".join(word)
word = re.sub(r'\W+', '', word)
columnnames = re.findall(r"(\w+):",str)
for c in columnnames:
c = re.sub(r'\W+', '', c)
data = "%s.%s," %( word,c)
w.write(data+"\n")
w.close()
h.close()