Unleash the Power of Python: Text Processing Made Simple!

Handling the text.


This section explores a number of handy use cases that usually arise when processing text with Python. By text files here we usually mean non-binary files containing text information without additional format info - .txt, .csv, .py, .html, etc.

Processing text files in Python is relatively easy to compare with most of the other programming languages.

Text processing with Python.
Text processing with Python meme.

Python Knowledge Base: Make coding great again.
- Updated: 2024-11-20 by Andrey BRATUS, Senior Data Analyst.



    In Python, there is no need for importing external libraries to read and write files. Python provides an inbuilt function for creating, writing, and reading files. Usually, we just use the “open()” function with reading or writing mode and then start to loop the text files line by line? which is even better using WITH context manager.



  1. Reading a textfile:


  2. 
    with open('myfile.txt', 'r') as file:
      content = file.read()
    
    print(content)  
    

    OUT: I am a file content.


  3. Creating new text file with your content:


  4. 
    content = """Welcome to
    
    www.python-code.pro"""
    
    with open('myfile1.txt', 'w') as file:
      file.write(content)
    
    print('My text added to the file !!!') 
    


    OUT: My text added to the file !!!


  5. Removing last character from text file:


  6. 
    with open('file.csv', 'r') as file:
      content = file.read()
    
    modified_content = content[:-1]
    
    with open('new-file.csv', 'w') as file:
      file.write(modified_content)
    
    print('new file without last character was created !!!') 
    

    OUT: new file without last character was created !!!


  7. Removing last character in several files:


  8. 
    from pathlib import Path
    
    files_dir = Path('files')
    
    for filepath in files_dir.iterdir():
      with open(filepath, 'r') as file:
        content = file.read()
        new_content = content[:-1]
      
      with open(filepath, 'w') as file:
        file.write(new_content)
        
    print('last character was successfully removed !!!') 
    

    OUT: last character was successfully removed !!!



  9. Replace a word in several files:


  10. 
    from pathlib import Path
    
    files_dir = Path('myfiles')
    
    for filepath in files_dir.iterdir():
      with open(filepath, 'r') as file:
        content = file.read()
        new_content = content.replace('word', 'phrase')
    
      with open(filepath, 'w') as file:
        file.write(new_content)
    print('word is replaced !!!')
    

    OUT: word is replaced !!!


  11. Merging text from several files:


  12. 
    from pathlib import Path
    
    files_dir = Path('myfiles')
    
    merged = ''
    for filepath in files_dir.iterdir():
      with open(filepath, 'r') as file:
        content = file.read()
      merged = merged + content + '\n'
    
    
    with open('result.csv', 'w') as file:
      file.write(merged)
    
    print('files are merged in a result file !!!')
    

    OUT: files are merged in a result file !!!


  13. Replacing text line in txt or csv file:


  14. 
    with open('original.csv', 'r') as file:
      content = file.readlines()
    
    content[0] = 'sex,drugs,rocknroll\n'
    
    with open('new.csv', 'w') as file:
      file.writelines(content)
    
    print('text line was replaced !!!') 
    

    OUT: text line was replaced !!!





See also related topics: