简体   繁体   English

.write()和.writelines()返回空白的.txt文件— PYTHON

[英].write() and .writelines() returning a blank .txt file — PYTHON

I have three string, numpy arrays (all of the same length) containing all the information I need. 我有三个包含所有所需信息的字符串numpy数组(长度均相同)。

I am trying to put the arrays together in a meaningful way in an empty text file I have defined as '1RESULTS.txt' 我试图以有意义的方式将数组放到我定义为'1RESULTS.txt'的空文本文件中

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Code

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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))

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If I substitute 'f.write' with 'print' the output is as follows. 如果将“ f.write”替换为“ print”,则输出如下。 This is what I would like 1RESULTS.txt to look like, but it remains blank. 这就是我想要的1RESULTS.txt,但它仍然为空白。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*****42469000730000*****

17456638--Gamma;

2271876.--Resistivity;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*****42469000840000*****

4881574.--Resistivity;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*****42469000850000*****

4881576.--Resistivity;
with open('1RESULTS.txt', 'w') as f:

here is your problem, you file is written over and over again each iteration deleting the previous entries. 这是您的问题,在每次迭代中都一遍又一遍地写入文件,删除了先前的条目。 You should rather append to the file with 您应该使用

with open('1RESULTS.txt', 'a') as f:

EDIT: better use the code as follows instead of opening and closing a stream so many times 编辑:最好使用以下代码,而不是多次打开和关闭流

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 is opened and rewritten every iteration. f会在每次迭代时打开并重写。 Thus only the last iteration affects the content of the file. 因此,只有最后一次迭代会影响文件的内容。 Change the 3rd and 4th lines to 将第三行和第四行更改为

with open('1RESULTS.txt', 'w') as f:
    (a, b, c) in zip(np_sub1, np_sub2, np_sub3):
       ...

and it should work as expected. 它应该可以按预期工作。

The with statement manages the context of the open function, which processes the file for you. with语句管理open函数的上下文,该函数将为您处理文件。 You shouldn't put it inside the loop because it will create a new context for each object of the iteration. 您不应将其放入循环中,因为它将为迭代的每个对象创建一个新的上下文。

Since you're opening the file in the 'w' mode, it will overwrite anything you wrote in the previous iteration. 由于您以“ w”模式打开文件,因此它将覆盖您在上一次迭代中编写的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM