Python 如何删除只有CRLF的行?

Python 如何删除只有CRLF的行?,python,python-3.x,Python,Python 3.x,昨天我的代码运行得很好,而今天它不工作了。很奇怪。我试图循环文件中的行,并删除所有只有CRLF或只有“#”(不带引号)的行 在下面的屏幕截图中,我想删除第一行和第二行,但不删除第三行 我尝试了一些组合,比如rstrip()和rstrip('\n')。现在,每次我都会留下一个空白文件。将变量“line”名称更改为(其冲突) 将变量“line”名称更改为(其冲突) 我对你的剧本做了一些修改。我只是对所有的\r\n使用一个普通的replace(),并检查清理后的行是否为空 with open('te

昨天我的代码运行得很好,而今天它不工作了。很奇怪。我试图循环文件中的行,并删除所有只有CRLF或只有“#”(不带引号)的行

在下面的屏幕截图中,我想删除第一行和第二行,但不删除第三行

我尝试了一些组合,比如
rstrip()
rstrip('\n')
。现在,每次我都会留下一个空白文件。

将变量“line”名称更改为(其冲突)

将变量“line”名称更改为(其冲突)


我对你的剧本做了一些修改。我只是对所有的
\r\n
使用一个普通的
replace()
,并检查清理后的行是否为空

with open('test.txt') as oldfile, open('test_cleaned.txt', 'w') as newfile:
    for line in oldfile:
        # within for loop
        cleaned_line = line.replace("\r\n", "")
        if cleaned_line == '#' or cleaned_line == '':
          # Ignore the lines that are blank or are just #
          continue
        newfile.write(cleaned_line + "\n")
print('DONE!!')

我对你的剧本做了一些修改。我只是对所有的
\r\n
使用一个普通的
replace()
,并检查清理后的行是否为空

with open('test.txt') as oldfile, open('test_cleaned.txt', 'w') as newfile:
    for line in oldfile:
        # within for loop
        cleaned_line = line.replace("\r\n", "")
        if cleaned_line == '#' or cleaned_line == '':
          # Ignore the lines that are blank or are just #
          continue
        newfile.write(cleaned_line + "\n")
print('DONE!!')
“现在每一次,我都会留下一个空白文件”。这是因为
line.rstrip()
总是在
line
中。我更喜欢从一开始就使用普通的旧弦

# File -> string
s_old = open('test1.py').read()
s_new = ''
for line in s_old.splitlines():
    if not line.startswith("#") and line.strip():                                      
        s_new += line
# String -> file
open('test2.py', 'w').write(s_new)
请注意,行中的
“#”与
行中的.startswith(“#”)
不同。而
line.strip()
将返回结尾带有换行符的行,因此,如果该行为空,则该空字符串将计算为false。

“现在每次,我都会留下一个空白文件”。这是因为
line.rstrip()
总是在
line
中。我更喜欢从一开始就使用普通的旧弦

# File -> string
s_old = open('test1.py').read()
s_new = ''
for line in s_old.splitlines():
    if not line.startswith("#") and line.strip():                                      
        s_new += line
# String -> file
open('test2.py', 'w').write(s_new)

请注意,
行中的
“#”与
行中的.startswith(“#”)
不同。而
line.strip()
则返回末尾带有换行符的行,因此如果该行为空,则该空字符串的计算结果为false。

行中的
line.rstrip()始终为true-
.rstrip()
返回其输入的子字符串,
中的
检查子字符串
not line.rstrip()
是检测完全由空白组成的行的合适条件。
line.rstrip()在
行中总是正确的-
.rstrip()
返回其输入的子字符串,
中检查子字符串
not line.rstrip()
是检测完全由whitespace.Lol组成的行的合适条件。我没意识到它在这么做。好的,现在有道理了。我试图简化代码,但还是得到了一个空文件:line1=“”if line.rstrip()或“#”in line else line1 newfile.write(line1)实际上,这些东西都不起作用。任何一行重复,所以我对所有的东西都有欺骗,或者空白的线条没有被删除。我必须切换到Excel。我可以在Excel.Lol中在5秒钟内完成此操作。我没意识到它在这么做。好的,现在有道理了。我试图简化代码,但还是得到了一个空文件:line1=“”if line.rstrip()或“#”in line else line1 newfile.write(line1)实际上,这些东西都不起作用。任何一行重复,所以我对所有的东西都有欺骗,或者空白的线条没有被删除。我必须切换到Excel。我可以用Excel在5秒钟内完成这项工作。哦,这确实像我希望的那样工作。非常感谢!!这是我的荣幸。顺便说一句,在excel宏中执行此操作时,python不适合这样做。我也会使用vim宏。但是“规范”的方法是在bash.sed中使用类似正则表达式的
cattest1.py | sed-e'/^\(#\ |$\)/d'
。然后你可以在pythonOh中使用
re
module,这确实是我希望的工作方式。非常感谢!!这是我的荣幸。顺便说一句,在excel宏中执行此操作时,python不适合这样做。我也会使用vim宏。但是“规范”的方法是在bash.sed中使用类似正则表达式的
cattest1.py | sed-e'/^\(#\ |$\)/d'
。然后可以在python中使用
re
模块