2014-03-28 70 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