Skip to Content
Menu
This question has been flagged
6 Replies
4932 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


Avatar
Discard
Best Answer

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...


Avatar
Discard
Author

Thank you for the answer Jusab.

Author

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 :(
Author

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 :)

Best Answer

Dear Dr Obx,

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

Best regards.

Avatar
Discard
Author

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.

Author Best Answer

Here is my solution : :)

if string: string =  string.encode('latin-1').decode('utf-8', 'ignore')
print '   My string      :',string
Avatar
Discard
Related Posts Replies Views Activity
1
May 21
2866
4
Oct 19
8647
1
Jan 18
7893
1
Feb 17
3902
0
Jan 17
3406