.write()和.writelines()返回一个空白的.txt文件--PYTHON
我有三个字符串numpy数组(长度都相同),包含我需要的所有信息 我试图以一种有意义的方式将数组放在一个空文本文件中,我已经定义为'1RESULTS.txt' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.write()和.writelines()返回一个空白的.txt文件--PYTHON,python,numpy,text-files,Python,Numpy,Text Files,我有三个字符串numpy数组(长度都相同),包含我需要的所有信息 我试图以一种有意义的方式将数组放在一个空文本文件中,我已经定义为'1RESULTS.txt' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ temp_str = ' ' temp_bool = False for (a, b, c) in zip(np_sub1, np_sub2, np_sub3):
temp_str = ' '
temp_bool = False
for (a, b, c) in zip(np_sub1, np_sub2, np_sub3):
with open('1RESULTS.txt', 'w') as f:
temp_bool = False
if a != temp_str:
f.write('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
f.write('*****' + a + '*****')
'\n'
f.write(b + '--' + c + ';')
temp_str = a
temp_bool = True
elif (temp_bool == False) and (a == temp_str):
f.write(b + '--' + c + ';')
'\n'
print('Type Unknown: ' + str(counter))
代码
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
temp_str = ' '
temp_bool = False
for (a, b, c) in zip(np_sub1, np_sub2, np_sub3):
with open('1RESULTS.txt', 'w') as f:
temp_bool = False
if a != temp_str:
f.write('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
f.write('*****' + a + '*****')
'\n'
f.write(b + '--' + c + ';')
temp_str = a
temp_bool = True
elif (temp_bool == False) and (a == temp_str):
f.write(b + '--' + c + ';')
'\n'
print('Type Unknown: ' + str(counter))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
temp_str = ' '
temp_bool = False
for (a, b, c) in zip(np_sub1, np_sub2, np_sub3):
with open('1RESULTS.txt', 'w') as f:
temp_bool = False
if a != temp_str:
f.write('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
f.write('*****' + a + '*****')
'\n'
f.write(b + '--' + c + ';')
temp_str = a
temp_bool = True
elif (temp_bool == False) and (a == temp_str):
f.write(b + '--' + c + ';')
'\n'
print('Type Unknown: ' + str(counter))
如果我将“f.write”替换为“print”,则输出如下。这是我希望1RESULTS.txt的外观,但它仍然是空白的
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*****42469000730000*****
17456638--Gamma;
2271876.--Resistivity;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*****42469000840000*****
4881574.--Resistivity;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*****42469000850000*****
4881576.--Resistivity;
这就是您的问题所在,您的文件在每次迭代中都会被一遍又一遍地写入,并删除以前的条目。您应该使用
with open('1RESULTS.txt', 'a') as f:
编辑:
最好按如下方式使用代码,而不是多次打开和关闭流
temp_str = ' '
temp_bool = False
with open('1RESULTS.txt', 'w') as f:
for (a, b, c) in zip(np_sub1, np_sub2, np_sub3):
temp_bool = False
if a != temp_str:
f.write('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
f.write('*****' + a + '*****')
'\n'
f.write(b + '--' + c + ';')
temp_str = a
temp_bool = True
elif (temp_bool == False) and (a == temp_str):
f.write(b + '--' + c + ';')
'\n'
print('Type Unknown: ' + str(counter))
这就是您的问题所在,您的文件在每次迭代中都会被一遍又一遍地写入,并删除以前的条目。您应该使用
with open('1RESULTS.txt', 'a') as f:
编辑:
最好按如下方式使用代码,而不是多次打开和关闭流
temp_str = ' '
temp_bool = False
with open('1RESULTS.txt', 'w') as f:
for (a, b, c) in zip(np_sub1, np_sub2, np_sub3):
temp_bool = False
if a != temp_str:
f.write('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
f.write('*****' + a + '*****')
'\n'
f.write(b + '--' + c + ';')
temp_str = a
temp_bool = True
elif (temp_bool == False) and (a == temp_str):
f.write(b + '--' + c + ';')
'\n'
print('Type Unknown: ' + str(counter))
f
将在每次迭代中打开并重写。因此,只有最后一次迭代才会影响文件的内容。将第3行和第4行更改为
with open('1RESULTS.txt', 'w') as f:
(a, b, c) in zip(np_sub1, np_sub2, np_sub3):
...
它应该像预期的那样工作
f
将在每次迭代中打开并重写。因此,只有最后一次迭代才会影响文件的内容。将第3行和第4行更改为
with open('1RESULTS.txt', 'w') as f:
(a, b, c) in zip(np_sub1, np_sub2, np_sub3):
...
它应该像预期的那样工作 with语句管理open函数的上下文,该函数为您处理文件。
您不应该将它放在循环中,因为它将为迭代的每个对象创建一个新的上下文
由于您正在以“w”模式打开文件,因此它将覆盖您在上一次迭代中编写的任何内容。with语句管理open函数的上下文,该函数为您处理文件。
您不应该将它放在循环中,因为它将为迭代的每个对象创建一个新的上下文
由于您以“w”模式打开文件,它将覆盖您在上一次迭代中编写的任何内容。您缺少
f.close()
在with
块中。@hoffee不,这不是问题所在。with
语句始终关闭文件。@hoffee使用with
时,无需显式调用f.close()
。这里的问题是每次迭代都会打开并重写f
。因此,该文件的内容是在上一次迭代中写入的,可能是空的。您的with
块中缺少f.close()
。@hoffee不,这不是问题所在。with
语句始终关闭文件。@hoffee使用with
时,无需显式调用f.close()
。这里的问题是每次迭代都会打开并重写f
。因此,文件的内容是在上一次迭代中编写的,可能是空的。我已经执行了您的建议和(@ducminh)下面的建议。由于某些原因,我仍然得到一个空白文件…但我的代码现在执行速度必须更快。是否有单独的open()子方法专用于使用“a”?感谢您到目前为止对Ishan的帮助,但是我仍然有一个空白的.txt文件。你对如何解决这个问题有什么进一步的建议吗?@Varmushu你介意指定文件的完整路径吗?以防万一我已经执行了你的建议和(@ducminh)下面的建议。由于某些原因,我仍然得到一个空白文件…但我的代码现在执行速度必须更快。是否有单独的open()子方法专用于使用“a”?感谢您到目前为止对Ishan的帮助,但是我仍然有一个空白的.txt文件。你对如何解决这个问题有什么进一步的建议吗?@Varmushu你介意指定文件的完整路径吗?以防我已经执行了你的建议和(@Ishan Srivastava)上面的建议。由于某些原因,我仍然得到一个空白文件…但我的代码现在执行速度必须更快。这个代码还有其他明显的缺陷吗?我已经执行了你的建议和(@Ishan Srivastava)上面的建议。由于某些原因,我仍然得到一个空白文件…但我的代码现在执行速度必须更快。这个代码还有其他明显的缺陷吗?