2013-06-21 36 views
1

我正在嘗試編輯一些文本文件,以便將值添加到其中一列。我想將兩個新數字添加到我的文件的第二列,其中用空格分隔。第一列將以13個字符結束,然後有兩個空格,然後添加新的兩個數字,其他列將保持不變。在Python中向文本文件的列添加新數字

我寫了下面的腳本,但不幸的是它確實有效。如果有人能幫我找到我的錯誤,我很感激。

%********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 
+1

你是什麼意思「它不工作」?有沒有錯誤?你得到了什麼,你應該得到什麼? –

+0

好吧,我會收到一個空的輸出。 – user2355306

+0

所以沒有寫入outfile?它不(也不應該)打印任何東西。你怎麼叫它? –

回答

1
with open('infile.txt', 'rb') as infile, open('outfile.txt', 'wb') as outfile: 
    outfile.writelines(line[:15] + '32' + line[15:] for line in infile) 
+0

:謝謝你的幫助。我已經試過你的代碼,它很好地工作,但只有一個問題是32之後的空間。我期望32應該是第二列的一部分,而不是一個新的列。你會用這種方式編輯它。 – user2355306

+0

@ user2355306,我更新了代碼。 – falsetru

2

使用str.split

col = 2 
#just pass filenames to your function and use `with` statement for handling files. 
with open(infile) as f, open(outfile, 'w') as out: 
    for line in f: 
     spl = line.split(None, col) 
     spl[col -1] = '32' + spl[col -1] 
     out.write(" ".join(spl)) 
...   
399299.855212 32512682.330 5403021.950 303.471 64 1 1 2  75 

399299.855212 32512681.470 5403020.790 302.685 1  1 2 2  75 

399299.855222 32512682.360 5403021.970 303.526 79 1 1 2  76 

工作代碼的版本:

def add_num(infile,outfile): 
    with open(infile) as f, open(outfile, 'w') as out: 
     output = ["%s %s%s\n" %(item.strip()[:13] ,32,item.strip()[16:]) for item in f] 
     out.writelines(output) 

outfile = os.path.join(folder, '{}_32{}'.format(base,extension)) 
add_num(infilename,outfile) 
+0

他原來的代碼工作正常......我確信他說錯了...... –

+0

謝謝你的親切幫助。所以我可以在上面的代碼中詢問我應該在哪裏定義我的輸出文件? – user2355306

+0

@ user2355306我已更新我的解決方案。 –

0

您可以使用片爲符號插入字符例如

myString = myString[:15] + " 12" + myString[15:] 

將在15位置

第一片myString[:15]插入4個字符將得到從一開始就串到第15位

第二片myString[15:]將從第15位獲得子到底

您可以將您的字符添加到中間來連接:

2
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 

add_num(open("infile.data"),open("outfile.data","w")) 

再看看outfile.data ......沒有錯,你的功能你可能怎麼叫它

+0

+1這工作正常。建議他使用'with'語句。 –

+0

是的,我只是想做一個快速的例子,顯示它工作正常......還有一些其他例子使用;) –

+0

我編輯了代碼,包括主代碼,以及請你看看它,如果通話有問題? – user2355306

相關問題