Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

How can i remove unwanted characters from string ?

Subscribe

Get notified when there's activity on this post

This question has been flagged
5 Replies
22340 Views
Avatar
Dr Obx

Already tried using: name = str.replace(name, ",","") but either i don't know what should I import into module or should i use different command

or getting an error: TypeError: descriptor 'replace' requires a 'str' object but received a 'list'

Would anyone tell me how  to do it ?

Quiet urgent ..... ;)

 

1
Avatar
Discard
Hiral Patel (hip)

Try this code: name = [i.replace(",", "") for i in name]

Dr Obx
Author
numbers = self.eangen()
numbers.append(self.calcheck(numbers))
res = ''.join(map(str, numbers))
record.write({'intcode': res})
Puzzle solved (partially) :)
Avatar
Anil Kesariya
Best Answer

Hi Dr Obx,

You can use strip() or replace() method.

str = " Anil Kesariya "
new_str = str.strip()
# o/p: Anil Kesariya

This method will remove unwanted space from left and right side of string.

If you want to remove the space or any symbolic pattern from whole string than you can use replace().

eg:1 remove space
str = " A n i l K e s a r i y a "
new_str = str.strip(" ","")
o/p : Anilkesariya
eg:2 remove "
str = Anil"kesari"ya'
new_str = str.replace('"',"")
o/p: Anilkesariya

Hope this will help.

Rgds,

Anil.






1
Avatar
Discard
Avatar
Rihene
Best Answer

Hello my friend;

Strings in python are immutable (can't be changed). Because of this, the effect of line.replace(...) is just to create a new string, rather than changing the old one. You need to rebind (assign) it to line in order to have that variable take the new value, with those characters removed.

Also, the way you are doing it is going to be kind of slow, relatively. It's also likely to be a bit confusing to experienced pythonators, who will see a doubly-nested structure and think for a moment that something more complicated is going on.

You can instead use str.translate: (https://docs.python.org/2/library/stdtypes.html#str.translate)

line = line.translate(None, '!@#$')

which only works on Python 2.6 and newer Python 2.x versions * —

or regular expression replacement with re.sub (https://docs.python.org/2/library/re.html#re.sub)

import re

line = re.sub('[!@#$]', '', line)

Here is an example:

s = "this is a string"

l = list(s) # convert to list

l[1] = "" # "delete" letter h (the item actually still exists but is empty)

l[1:2] = [] # really delete letter h (the item is actually removed from the list)

del(l[1]) # another way to delete it

p = l.index("a") # find position of the letter "a"

del(l[p]) # delete it

s = "".join(l) # convert back to string

Timing comparison

def findreplace(m_string, char):

m_string = list(m_string)

for k in m_string:

if k == char:

del(m_string[m_string.index(k)])

return "".join(m_string)

def replace(m_string, char):

return m_string.replace("i", "")

def translate(m_string, char):

return m_string.translate(None, "i")

from timeit import timeit

print timeit("findreplace('it is icy','i')", "from __main__ import findreplace")

print timeit("replace('it is icy','i')", "from __main__ import replace")

print timeit("translate('it is icy','i')", "from __main__ import translate")

Result

1.64474582672
0.29278588295
0.311302900314

str.replace and str.translate methods are 8 and 5 times faster.

Regards.

0
Avatar
Discard
Dr Obx
Author

Gr8 thanx. I'm a beginner so every information matter ;) I'll take it into my head so next time I'll try your ideas and methods ;) Thank you

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now