2014-03-28 66 views
1

我想從文件中取數字,給它們分配一個變量名,對變量做一些數學運算,然後寫入文件中的下一行。例如,如果我有一個文件:1 2 3 4 5,那麼這裏是我到目前爲止的縮寫代碼(Python 3.3)。我遇到的唯一問題是將計算結果寫入下一行。預先感謝您的幫助。寫入文件下一行

with open('test.txt', 'r') as f: 
    read_data = f.read() 
    a1 = (read_data[0])`a1 = (read_data[0]) 
print(a1) # this is just a test to see whats happening 
f.close() 
with open('test.txt','w') as f: 
    f.write(a1) #how do I get a1 to write on the next line of the file 
exit() 
+2

我認爲用''a''而不是''w''打開文件應該有幫助。該文件附加到文件而不是覆蓋。 – elParaguayo

回答

0
with open('test.txt', 'r') as f: 
    data = f.read() 
with open('test.txt', 'w') as f: 
    for line in data.split('\n'): 
     a1 = line # You didn't specify what modifications you did with the data so... 
     f.write(line + '\n') 
     f.write(a1 + '\n') 

另外請注意,我離開了f.close(),因爲你不需要它。
忘了它叫什麼(旁邊的上下文管理器旁邊的另一個花哨詞)但with是一個自動關閉語句,每當你離開塊f將有.close()由Python自動調用。

既然你沒有寫你想要做什麼樣的calulcations的,這裏有一個由例如:

with open('test.txt', 'r') as f: 
    data = f.read() 
with open('test.txt', 'w') as f: 
    for line in data.split('\n'): 
     a1 = int(line)*100 
     f.write(line + '\n') 
     f.write(str(a1) + '\n') 

而且從你的下一行提到的例子讀書,但沒有指定如果該數據已經由線分離,所以服用的答案從djas你可以合併到這些:

with open('test.txt', 'r') as f: 
    data = f.read() 
with open('test.txt', 'w') as f: 
    for line in data.split(): 
     a1 = int(line)*100 
     f.write(line + '\n') 
     f.write(str(a1) + '\n') 
+0

謝謝,但是將a1的值放在文件第一行的末尾,希望將a1的值放在下一行的開頭。 – user3472574

+0

@ user3472574我明白了,所以你想讀第一行,做點什麼,把它寫到第二行,然後讀取以前是第二行aaa和重複的內容?如果是這樣的話,如果你展示了你想要做什麼樣的「工作」,將會有所幫助,因爲上面的代碼沒有任何意義:a1 =(read_data [0])'a1 = ...'這很奇怪。 – Torxed

0

假設輸入文件看起來像

04 80 52 67 

,下面的代碼工作:

with open('input.txt', 'r') as f: 
    line = f.read() 

with open('output.txt','w') as f: 
    for element in line.split(): 
     f.write("%s\n" % ''.join(element)) 

編輯:你也可以更換f.write("%s\n" % ''.join(element))f.write(element+'\n')和去除f.close()由@Torxed的建議。

+1

也可以跳過'.close()':)因爲這是'with'的全部要點。 – Torxed

0

with open會自動關閉該文件,因此您可以在with open聲明中執行程序的主邏輯。例如,該程序會做你的需要:

with open('test.txt', 'r') as f: 
    lines = f.readlines() 
newlines = [] 
for line in lines: 
    newlines.append(line) 
    newline = #do some logic here 
    newlines.append(newline) 
with open('test.txt','w') as f: 
    for line in newlines: 
    f.write(line + '\n') 

如果只有一條線,可以消除for line in lines:循環,直接修改該文件來代替。另一方面,如果您希望這是一個迭代過程,如其中一個問題的評論所示 - 也就是說,您希望新的第二行成爲新的第一行,以此類推,您會把上面的一些功能如下:

def func(first_line_index): 
    """ 
    Takes the index of the current first line as first_line_index and modifies 
    the next line as per your assignment, then writes it back to the file 
    """ 
    with open('test.txt', 'r') as f: 
    lines = f.readlines() 
    line1 = lines[first_line_index] 
    newlines = [] 
    for line in lines[:first_line_index+1]: 
    newlines.append(line) 
    newlines.append(lines[first_line_index]) 
    newline = #do some logic here based on the line above 
    newlines.append(newline) 
    for line in lines[first_line_index+2:]: 
    newlines.append(line) 
    with open('test.txt','w') as f: 
    for line in newlines: 
     f.write(line + '\n') 

def __main__(): 
    counter = 0 
    while #some condition- depends on your assignment: 
    func(counter) 
    counter += 1