Python:以单词反转字母,就像在镜子中看到的单词| Neculai Fantanaru(en)
ro  fr  en  es  pt  ar  zh  hi  de  ru
ART 2.0 ART 3.0 ART 4.0 ART 5.0 ART 6.0 Pinterest

Python:将字母置换为单词,就像在镜子中看到的单词一样

On May 05, 2021, in Leadership and Attitude, by Neculai Fantanaru

您可以在此处查看完整代码:HTTPS://帕萨特斌.com/3PN甲骨文NC

import re

def read_text_from_file(file_path):
    """
    This function returns the contents of a file.
    """
    with open(file_path, 'r') as f:
        text = f.read()
        return text

def write_to_file(text, file_path):
    """
    This function writes text to a file.
    text: the text you want to write
    """
    with open(file_path, 'w') as f:
        f.write(text)

def reverse_words(file_path):
    """
    This function inverts all words in a file.
    """
    
    text = read_text_from_file(file_path)
    words = set(re.findall(r'[A-Za-z0-9\-\']+', text))
    for word in words:
        reversed_word = ''.join(reversed(word))
        text = text.replace(word, reversed_word)
    # we write the modified text in the file !!! attention, the content is overwritten !!!
    write_to_file(text, file_path)

if __name__ == '__main__':
	# the path of your file
    file_path = 'd:\Exercises\1'   
    reverse_words(file_path)

Other examples:

https://superuser.com/questions/1684040/

https://community.notepad-plus-plus.org/topic/22054/

That's all folks.

If you like my code, then make me a favor: translate your website into Romanian, "ro".

此外,有一个版本2.这个代码或版本3.要么版本4.要么版本5.要么版本6.


Latest articles accessed by readers:

  1. An Eye To See And A Mind To Understand
  2. Turn Towards Me With An Eye Full Of Your Own Gaze
  3. The Snapshot Of Magic In God's Universe
  4. Rhythm Of My Heart

Donează prin Paypal

Alternate Text

DONAŢIE RECURENTĂ

Donează lunar pentru susţinerea proiectului NeculaiFantanaru.com

DONAŢIE SINGULARĂ

Donează suma dorită pentru susţinerea proiectului NeculaiFantanaru.com

Donează prin Transfer Bancar

Cont Lei: RO34INGB0000999900448439

Deschis la ING Bank

Alatura-te Comunitatii Neculai Fantanaru

decoration
About | Site Map | Partners | Feedback | Terms & Conditions | Privacy | RSS Feeds
© Neculai Fântânaru - All rights reserved