[英]How do I strip all letters, whitespaces and empty lines from a txt file and write to excel
I have a file like this我有一个这样的文件
cat
123-1
2311
I want it to be like this with no leading or trailing white spaces or empty lines我希望它是这样的,没有前导或尾随空格或空行
123-1
2311
with open(r"C:\Users\mike\test.txt", 'r', encoding="utf8") as f:
readline= f.readlines()
#clearUnwantedCharacters= ''.join(filter(clearlist.__contains__,readline))
#readline= ''.join(filter(clearlist.__contains__,readline))
readline =[line.replace(' ', '') for line in readline]
#readline =[line.replace('\n', '') for line in readline]
#######
lines= readline.split("\n")
no_empty_string= [line for line in lines if line.strip() !=" "]
'''
since list cannot be split, split individual items on the list and append
'''
readline =""
for line in no_empty_string:
readline += line + "\n"
######
#test= readline
#remove unwanted characters with regex
# readline= re.sub('[^0-9]',' ', str(readline))
# readline.strip(' ')
print(readline)
with open(r"C:\Users\mike\test.txt", 'w', encoding="utf8") as f:
f.writelines(readline)
But I don't get the desired result ( ie not been able to clean the list of white spaces and letters)但我没有得到想要的结果(即无法清理空格和字母列表)
-- updating question with more information, i would like to write the text to excel, so instead of a newline, comma separated lines would be ideal - 用更多信息更新问题,我想将文本写入excel,因此最好用逗号分隔行而不是换行符
# Opening both files, iterating over the lines using .strip()
# and saving only lines that are not empty.
with open('test.txt', "r") as original, open("new_file.txt", "w") as new_file:
for line in original:
sline = line.strip()
if line not in ('', '\n'):
new_file.write(sline + "\n")
$ cat new_file.txt
123-1
2311
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.