0

i'm going straight to the point; First of all i'm trying to select all lines if i find one char in common e.g:

-"activit�"

-"activit�s"

-"actrice"

-"actualisation"

I'd like to select all lines where � is present, then i'd like to make a python script removing all words who have less or more than 5 chars

with open("liste_francais.txt") as mot:
    for a in readlines(mot):
            if len(a)!=5:
                a.replace(a, "")

this is my code atm but its not working, may i get help ? :)

rioV8
  • 24,506
  • 3
  • 32
  • 49
Darleanow
  • 31
  • 4
  • You're not writing the changes, you're just reading in the file but never writing anything. You'll have to actually [modify the file](https://stackoverflow.com/questions/125703/how-to-modify-a-text-file). – Random Davis Apr 26 '22 at 22:04
  • you better find the encoding of the file and use that to open/read the content, then you deal with the `strange` characters – rioV8 Apr 26 '22 at 22:10
  • 1
    .replace return a copy of the string with the replacements done to it (if any) it doesn't affect either the original string or the file where it came from... – Copperfield Apr 27 '22 at 17:48
  • added the answer below :) – Darleanow May 02 '22 at 22:57

1 Answers1

0
import unidecode #e.g. é -> e
lst=[]
with open("liste_francais.txt") as file:"""opening txt file and getting each item from it"""
    lines=file.readlines()
    for a in lines:
            if len(a)==6:"""\n adds up one char so if u want n chars do n+1"""
                lst.append(a)
textfile = open("a_file.txt", "w")"""new txt file"""
for element in lst:
    element = unidecode.unidecode(element)"""é to e"""
    textfile.write(element.upper()+"\n")"""upper for readibility"""
textfile.close()
Darleanow
  • 31
  • 4
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 27 '22 at 23:47
  • is it better now ? – Darleanow May 02 '22 at 22:57