简体   繁体   中英

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

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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