If you come from a Java background to Python, you might be looking for an alternative for .replace() to work with RegEx.
In the module that you need is re, this module is full of regular expression matching operations similar to Pattern and Matcher utils in Java.
Let us take a look at some examples.
Example: Let's say you have a string "Hello, Java!" that you want to replace Java with Python
import re
message = "Hello, Java!"
new_message = re.sub(r"Java", "Python", message)
print(new_message)
Output:
We have made use of the re.sub() method
Syntax:re.sub(pattern, repl, string, count=0, flags=0)
Now let us take a look at a practice example of working with CSV files.
data.csvName,Age,Email,City
Mike Alan,25,mike@example.com,New York
Steve Yeli,30,steve@example.com,San Francisco
Stacy John,35,stacy@example.com,Chicago
Now let's try to extract the city names from the data.csv
import csv
csv_file = 'data.csv'
with open(csv_file, 'r') as file:
reader = csv.reader(file)
next(reader)
for row in reader:
if len(row) >= 4:
city = row[3]
print(city)
Facing issues? Have Questions? Post them here! I am happy to answer!
- Python List of Lists with Examples
- Fix: ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter
- How to Convert Python Notebook .ipynb (JSON) to Executable .py Module
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Pandas Merge Indicator (Left, Right and Both) Example
- Fix: EOFError Exception in Python
- How to URL Decode a Query String in Python
- How to take user input from the console in a Python program
- Compare two lists in Python and return matches
- How to change the Python Default version
- Fix: KeyError: exception in Python
- How to get the Execution Time of A Python Program
- Fix: ModuleNotFoundError: No module named boto3 [Python]
- How to get unique values from a list in Python
- How to pip install Python Modules in VSCode
- Python: Fix - TypeError: NoneType object is not iterable
- How to delete a file using Python code example
- How to create a dictionary comprehension in Python
- Python: Get just the filename without extension using Path
- How to comment out a block of code in Python
- How to add two float numbers in Python
- Python: How to POST Json Data with HTTP Request
- Get MD5 Hash as Checksum of a String in Python
- Python - Convert float to String
- How to Parse XML String in Python
- Drag drop files here option missing for SharePoint document library - SharePoint
- New-SPLogFile PowerShell - create new SharePoint log file - SharePoint
- Portable Notepad++ for windows - NotepadPlusPlus
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass - Java
- Fix Generics: error unexpected type required: class found: type parameter - Java
- [fix] java: incompatible types: double cannot be converted to java.lang.Integer Generics - Java
- SharePoint Server 2016 setup error - A system restart from a previous installation or update is pending. Restart your computer and run setup to continue. - SharePoint
- How to Mute All Sounds on Notepad++ on Windows - NotepadPlusPlus