简体   繁体   English

如何从 txt 文件中删除所有字母、空格和空行并写入 excel

[英]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.

相关问题 我有一个包含几行的.txt 文件。 我必须从中删除所有带有字母“a”的行并将其写入另一个文件。 我该怎么做? - I've got a .txt file with a few lines. I have to remove all the lines with the letter "a" from it and write it to another file. How do I do it? 从空行中删除文件 - strip a file from empty lines 如何从 CSV 文件中删除所有字母? - How do I remove all the letters from a CSV file? 如何使用CSV文件中的数据在txt文件中写入行 - How to write lines in a txt file, with data from a csv file 如何写成多行的txt文件? - How can i write to a txt file with multiple lines? 从txt读取行。 文件,并将除第6行外的所有行写在另一行上,然后刷新第一个文件 - Read lines from a txt. file and write all except 6 first lines on another, then flush the first file 如何删除txt文件中的“空”行 - How to delete 'empty' lines in txt file 如何编写 python 从名为“file1.txt”的文本文件中读取前两行 将从“file1.txt”读取的两行写入新文件“file2.txt” - How write python to Read the first two lines from a text file named "file1.txt" Write the two lines read from "file1.txt" to a new file "file2.txt" 如何去除文件中文本中的\\n,然后计算python中超过n个字母的文本单词? - How do I strip the \n in a text in a file and then count the words of the text that have more than n letters in python? 如何检查 txt 文件是否仅包含空格 - How can I check if a txt file only contains whitespaces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM