Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


How to cleanup string read from CSV containing non ascii codes?

Dr Obx
on 3/2/16, 11:56 AM 291 views

Guys, importing data from CSV, amongst them there is a field which contain a price but the price is not just a numbers but also currency sign.

So I was trying to remove this sign using replace, trim, re etc but it won't go away :)

The reason why I would like to remove is the field total_price is a float type so I can't write non float values.

How can I do it, how to remove it ?

if record.total_price:

    total_price = record.total_price.replace('£','')

    print ' Total price :',total_price

if record.vat_rate:

    vat_rate = record.included_vat_rate

    print ' VAT rate :',vat_rate


Jusab Sida (jsi)

--Jusab Sida (jsi)--
Tiny ERP Pvt Ltd
| 2 1 5
Gandhinagar, India
--Jusab Sida (jsi)--

Jusab Sida (jsi)
On 3/2/16, 1:43 PM

Here suppose you read a price as a string then you can do like...

symbolic_price = "$ 786.92"
symbol_and_price = symbolic_price.split('$')

print 'My price : ',float(symbol_and_price[1])

Hope this will help you little bit...

Thank you for the answer Jusab.

Dr Obx
on 3/2/16, 4:24 PM

But it's not as simple as it look like ;)

if record.total_price:
	total_price =  record.total_price.split('£')
	print '   Total price      :',float(total_price[1])
and as a result I'm getting: Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 530, in _handle_exception return super(JsonRequest, self)._handle_exception(exception) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 567, in dispatch result = self._call_function(**self.params) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 303, in _call_function return checked_call(self.db, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 113, in wrapper return f(dbname, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 300, in checked_call return self.endpoint(*a, **kw) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 796, in __call__ return self.method(*args, **kw) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 396, in response_wrap response = f(*args, **kw) File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 948, in call_button action = self._call_kw(model, method, args, {}) File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 936, in _call_kw return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper return old_api(self, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 363, in old_api result = method(recs, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/addons/iprodstep_log/model/iprodstep_log.py", line 484, in order_sort total_price = record.total_price.split('£') UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128) this error :) I already tried decode it, encode it with no luck which is weird because everything I've done should change, remove, or replace unwanted sign but it won't :(
Dr Obx
on 3/2/16, 4:29 PM

As you can see, the difference between my code and your example is ... the currency sign :) If it's '$' there will not be a problem. But because the "Pound" sign is not from range(128) it just won't work that way :)

Dr Obx
on 3/2/16, 4:32 PM

Dear Dr Obx,

Would not it be better to edit the CSV file and delete the symbol?

Best regards.

Hahahaha not really, one thing, the person who is loading data into the system is not a person who "know" how to do it, second thing is .... I don't want to take a risk if somebody by mistake while editing change some other data and import file into the system ;) so No, better no. That's i was struggling to find a solution which will do it for me while the file is loaded. Problem solved as I showed below.

Dr Obx
on 3/3/16, 4:52 AM
Dr Obx
On 3/2/16, 6:13 PM

Here is my solution : :)

if string: string =  string.encode('latin-1').decode('utf-8', 'ignore')
print '   My string      :',string

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 3/2/16, 11:56 AM
Seen: 291 times
Last updated: 3/3/16, 4:05 AM