简体   繁体   中英

Remove whitespace before a specific character in python?

I was wondering if you knew the best way to do this.

This program uses OCR to read text. Occasionally, spaces appear before a decimal point like so:

{'MORTON BASSET BLK SESAME SEE': '$6.89'}
{"KELLOGG'S RICE KRISPIES": '$3.49'}
{'RAID FLY RIBBON 4PK': '$1 .49'}

as you can see, a space appears before the decimal point on the last entry. Any ideas on how to strip JUST this whitespace?

Thank you :)

EDIT: contents before decimal point may contain a varying amount of whitespace. Like

$1    .49
$1  .49
$1 .49

Use regular expressions .

import re
a_list = {"1 .49", "1   .49", "1          .49"}

for a in a_list:
  print re.sub(' +.', '.', a)

Result will be

1.49
1.49
1.49

You can just strip out all whitespace from the string, assuming that they follow the same format. SOmething like this:

for item in items: 
  for key in item.keys():
    item[key] = item[key].replace(" ", "")

The key part is replacing the whitespace with no whitespace.

If you just want the whitespace before the ".", then you could use: .replace(" .", ".") instead.

This would only replace 1 white space. To replace multiple, you could use a while loop like this:

while ' .' in item[key]:
  item[key].replace(' .', '.')

For your dict obj:-

>>> d = {'RAID FLY RIBBON 4PK': '$1 .49'}
>>> d['RAID FLY RIBBON 4PK'] = d['RAID FLY RIBBON 4PK'].replace(' ','')
>>> d
{'RAID FLY RIBBON 4PK': '$1.49'}

Even if there is varying space; replace would work fine. See this:-

>>> d = {'RAID FLY RIBBON 4PK': '$1       .49'}
>>> d['RAID FLY RIBBON 4PK'] = d['RAID FLY RIBBON 4PK'].replace(' ','')
>>> d
{'RAID FLY RIBBON 4PK': '$1.49'}

i think that maybe you want something more generic not only for that key:

for key, value in d.items():
   d[key]=value.replace(" ","")

in this way independent of the key othe number of space the result will be without white spaces

This is trivial with split and join:

"".join("1     .49".split())

This works because splits on one or more spaces. To do this for each value in a dictionary:

{k, "".join(v.split()) for k,v in dict_.items()}

当然:

string.replace(' .', '')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM