[英]remove empty lines, dots and commas from textfile
I want to remove empty lines, dots and commas from my textfile.我想从我的文本文件中删除空行、点和逗号。 I am not really sure how to do it.
我不确定该怎么做。 I tried different metods but didnt get any results
我尝试了不同的方法,但没有得到任何结果
filename = "phil.txt"
def countLines():
"""
Read number of lines in the text file.
"""
numOflines = 0
with open(filename) as file:
for line in file:
numOflines += 1
print("Total number of lines is: ", numOflines)
return numOflines
countLines()
I get 19 lines but the answer should be 17 lines.我得到 19 行,但答案应该是 17 行。 I addition I want to also remove commas and dots for later use.
我还想删除逗号和点以备后用。
Please check the following solution and follow comments in code:请检查以下解决方案并遵循代码中的注释:
def clean(lines):
"""remove empty lines, commas and dots """
new_lines = [] # create output list for new file
for line in lines:
if line != "\n": # if not empty line let's check for . and ,
new_line = "" # basis for new line
for letter in line: # check letter in line
if letter == "." or letter == ",":
new_line += "" # if you want space just use " " instead of ""
else:
new_line += letter # otherwise just add old letter
new_lines.append(new_line)
return new_lines
if __name__ == "__main__":
lines = [] # read lines from file
with open("testfile.txt", mode="r") as f: # read all lines from file
for i in f:
lines.append(i)
with open("testfile.txt", mode="w") as f: # open file again to rewrite
for line in clean(lines): # clean lines of all file
f.write(line) # write new lines to file
I hope that you problem is solved, feel free to ask questions.我希望你的问题得到解决,随时提出问题。
I'd do something like:我会做类似的事情:
replaced_characters = {'\r\n': '\n', '\n\n': '\n', ',': ' ', '.':' ', ' ': ' '}
with open('file.ext', 'r') as f:
text = f.read()
for k, v in replaced_characters.items():
text = text.replace(k, v)
print(text)
I haven't tested it.我没有测试过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.