列の 1 つに値を追加するために、いくつかのテキスト ファイルを編集しようとしています。スペースで区切られたファイルの 2 番目の列に 2 つの新しい数字を追加したいと思います。最初の列は 13 文字で終了し、2 つのスペースがあり、新しい 2 桁を追加すると、他の列は変更されずに残ります。
次のスクリプトを作成しましたが、残念ながら機能します。誰かが私の間違いを見つけるのを手伝ってくれたら、私は感謝しています.
%********function************
def add_num(infile,outfile):
output = ["%s %s%s" %(item.strip()[:13] ,32,item.strip()[16:]) for item in infile]
outfile.write("\n".join(output))
outfile.close()
return outfile
%*********************************
%**********main code for calling the function*******
import os, Add32
folder = 'E:/MLS_HFT/TEST/Stuttgart_2009_pointclouds/'
for filename in os.listdir(folder):
infilename = os.path.join(folder,filename)
if not os.path.isfile(infilename): continue
base,extension = os.path.splitext(filename)
infile= open(infilename, 'r')
outfile = open(os.path.join(folder, '{}_32{}'.format(base,extension)),'w')
add32.add_num(infile,outfile)
これはデータのサンプルです:
399299.855212 512682.330 5403021.950 303.471 64 1 1 2 75
399299.855212 512681.470 5403020.790 302.685 1 1 2 2 75
399299.855222 512682.360 5403021.970 303.526 79 1 1 2 76