简体   繁体   English

如何删除文本文件python中的空白行?

[英]How to remove blank lines in text file python?

In my python script, I write specific columns from a text_file to a new_text_file separated by , because the new_text_file will later become a csv_file. 在我的python脚本中,我将特定的列从text_file写入由分隔的new_text_file ,因为new_text_file稍后将成为csv_file。 There are white space lines left over in the new_text_file because of lines I skipped writing over that need to be removed from the file. 由于我跳过了一些需要从文件中删除的行,因此new_text_file中留有空白行。

I can't use .strip() or .rstrip() because I get the error: AttributeError: '_io.TextIOWrapper' object has no attribute 'strip' . 我无法使用.strip().rstrip()因为出现错误: AttributeError: '_io.TextIOWrapper' object has no attribute 'strip'

I can't use ip_file.write("".join(line for line in ip_file if not line.isspace())) because I get the error: UnsupportedOperation: not readable . 我无法使用ip_file.write("".join(line for line in ip_file if not line.isspace()))因为我收到错误: UnsupportedOperation: not readable

I also tried importing sys and re , and have tried every other answer found on this site, but it still returns errors. 我也尝试导入sysre ,并尝试了在此站点上找到的所有其他答案,但是它仍然返回错误。

My code is: 我的代码是:

for ip in open("list.txt"):
    with open(ip.strip()+".txt", "a") as ip_file:
        for line in open("data.txt"):
            new_line = line.split(" ")
            if "blocked" in new_line:
                if "src="+ip.strip() in new_line:
                    #write columns to new text file
                    ip_file.write(", " + new_line[11])
                    ip_file.write(", " + new_line[12])
                    try:
                        ip_file.write(", " + new_line[14] + "\n")
                    except IndexError:
                        pass

The resulting ip_file looks like: 生成的ip_file看起来像:

, dst=00.000.00.000, proto=TCP, dpt=80
, dst=00.000.00.000, proto=TCP, dpt=80
, dst=00.000.00.000, proto=TCP, dpt=80

, dst=00.000.00.000, proto=TCP, dpt=80
, dst=00.000.00.000, proto=TCP, dpt=80

I was coding under the last line of the above script, within the loops. 我在循环中在上述脚本的最后一行下进行编码。 The new_text_file is ip_file in my script and everything must be in Python. 在我的脚本中, new_text_fileip_file ,所有内容都必须在Python中。

Question: Is there another way to remove the blank lines in ip_file ? 问题:还有另一种方法可以删除ip_file中的空白行吗? OR prevent them from ever being written? 还是阻止他们被写?

I think I understand what you're saying. 我想我明白你的意思。 Try making these changes: 尝试进行以下更改:

        for line in open("data.txt"):
            new_line = line.rstrip().split()
                                    ^^^^^^^^^
            if "blocked" in new_line:
                if "src="+ip.strip() in new_line:
                    #write columns to new text file
                    ip_file.write(", " + new_line[11])
                    ip_file.write(", " + new_line[12])
                    try:
                        ip_file.write(", " + new_line[14])
            #                                                      ^^^^
                    except IndexError:
                        pass
                    ip_file.write("\n")
            #           

It seems that the problem was that when new_line[14] existed, it already contained a newline, so you were appending two newlines. 似乎问题是当new_line[14]存在时,它已经包含换行符,因此您要追加两个换行符。 The above code rstrips any newline off line before you split it, then appends a single newline no matter what at the end of the inner for loop. 上面的代码在拆分之前将所有换行符脱机,然后在内部for循环的末尾附加一个换行符。

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

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