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!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
- How to open CMD for current file/folder location in Notepad++ - NotepadPlusPlus
- [Solution] Installing Whatsapp There's insufficient space on the device - WhatsApp
- Error : Facebook SDK AndroidRuntime?FATAL EXCEPTION: main - Android
- Spotlight Search Mac Keyboard Shortcut - MacOS
- Read file from resources folder in Java project code - Java
- How to run .sh file in Mac Terminal - MacOS
- JavaScript : Get current page address - JavaScript
- [fix] Microsoft Error Code 0x80004005 - Unspecified error - Microsoft