0

I saw too much question here no one answered my question. I have text file ckb.txt :

چ
چۆنی
باشی
سوپاس
ئاسمان
یاسا
دەنگ
چنار
کورستانی گەورە

I want to remove any line which len() is more than 4 characters, I wrote this simple code but not working, Any suggestion or tip?

    import arabic_reshaper
    from bidi.algorithm import get_display
    import unicodedata

    with open("ckb.txt", 'r+', encoding='utf-8') as f:
    
        g = f.readlines()
        for line in g:
            print(get_display(arabic_reshaper.reshape(line)))
            print(len(line))
            if len(line) > 4:
                del line
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
Jwtiyar
  • 43
  • 7
  • What do you mean by "remove"? from the file? from a variable? What do you mean by "not working"? What *does* it do? – Sayse Oct 19 '20 at 08:04
  • Does this answer your question? [How to delete a specific line in a file?](https://stackoverflow.com/questions/4710067/how-to-delete-a-specific-line-in-a-file) – Tomerikoo Oct 19 '20 at 08:05
  • I think this can help you https://stackoverflow.com/q/4710067/14454560 – me.nkr Oct 19 '20 at 08:10
  • @Sayse i want to delete the word when the condition is true. – Jwtiyar Oct 19 '20 at 08:18
  • @Tomerikoo I already saw this but it's better if you have specific word not checking all words to delete the one that condition not applied. – Jwtiyar Oct 19 '20 at 08:18
  • In the accepted answer, you just need to change `if line.strip("\n") != "nickname_to_delete":` to `if len(line) < 4:`... – Tomerikoo Oct 19 '20 at 08:19
  • @Tomerikoo Let's you have a file have 5 line each of them have a word, if you want to delete the line that have word which its characters is more than 4 . – Jwtiyar Oct 19 '20 at 08:20
  • Delete from *what*? – Sayse Oct 19 '20 at 08:20
  • Again, the accepted answer in the link is the answer you're looking for, with the change I wrote above... Please close this question as duplicate – Tomerikoo Oct 19 '20 at 08:21
  • @Tomerikoo I did but deleted all the lines. – Jwtiyar Oct 19 '20 at 08:23
  • Try `if len(line.strip()) < 4:`. Otherwise, all your lines are `>= 4`... – Tomerikoo Oct 19 '20 at 08:26
  • @Tomerikoo I know this should solve it but it deletes all the lines and make it emplty file. my words are not all more than 4 as you can see first one is 1 character last one is 15 i think. – Jwtiyar Oct 19 '20 at 08:34
  • Well that's as far as I can help man... Maybe it has something to do with the arabic characters? I can't reproduce as they are being copied as squares... – Tomerikoo Oct 19 '20 at 08:36
  • @Tomerikoo If you mean python they should be encode it with utf-8. – Jwtiyar Oct 19 '20 at 08:41

3 Answers3

3

Based on this: How to delete a specific line in a file?

import arabic_reshaper
from bidi.algorithm import get_display
import unicodedata

with open("ckb.txt", 'r+', encoding='utf-8') as f:

    g = f.readlines()
    f.seek(0)
    for line in g:
        print(get_display(arabic_reshaper.reshape(line)))
        print(len(line))
        if len(line.strip("\n")) <= 4:
            f.write(line)
    f.truncate()
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
ky_aaaa
  • 290
  • 3
  • 10
  • It adds all the line agian and again – Jwtiyar Oct 19 '20 at 08:09
  • That's not what is asked for... OP wants to **delete** lines, not add them again – Tomerikoo Oct 19 '20 at 08:09
  • After you do `f.readlines()` the file pointer is at the end of the file... Then every following `write` just adds lines at the end of the file. The final `f.truncate` does nothing... Maybe you meant to do: `g = f.readlines(); f.seek(0); f.truncate()` and the rest stays the same. That would work... – Tomerikoo Oct 19 '20 at 08:12
2
with open("ckb.txt", 'r+', encoding='utf-8') as f:
    lines = f.readlines()

with open("ckb.txt", 'w+', encoding='utf-8') as f:
    lines = list(filter(lambda x: len(x) <= 4, lines))
    f.writelines(lines) 
Danis
  • 344
  • 2
  • 13
1

This should do the trick:

lines = [line for line in open("ckb.txt", 'r+', encoding='utf-8').readlines() if len(line) < 4]

Use it directly after importing libraries.

If you want to write back these lines in original file, use:

with open("ckb.txt", 'w', encoding='utf-8'):
    f.write('\n'.join(lines))
Hamza
  • 5,373
  • 3
  • 28
  • 43