-2

I am trying to copy a string from a website and paste it into another program. But I first have to remove the "." and "-" from the string. Ex. it is "123.345.322.22-00" but I need it to be "1233453222200".

I have tried using replace() and replace and join(). It is python 3.7.2

#copy number
pg.moveTo(238,419)

pg.click(238,419,clicks=3)
pg.hotkey('ctrl','c')

cep = pyperclip.paste()

print(cep)



cepnovo= [cep.split(".").join("")]


print(cepnovo)

I get AttributeError: 'list' object has no attribute 'join'

actual result AttributeError: 'list' object has no attribute 'join'

Expected is to output the string without "." and "-".

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 5
    `join` is a `str` method that takes an iterable, not a `list` method: `"".join(cep.split("."))` – Graipher Mar 25 '19 at 15:03
  • Use `replace`: `cep.replace('.').replace('-')` or `re.sub`: `import re ; cep = re.sub('[\.-]', '', cep)` – DeepSpace Mar 25 '19 at 15:04
  • 2
    "I have tried using replace()" Please show us that code. – MisterMiyagi Mar 25 '19 at 15:05
  • Possible duplicate of [Remove specific characters from a string in Python](https://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python) – Hugo Mar 25 '19 at 15:19

5 Answers5

5

You say you tried replace, but fail to give what happens. Because this works:

>>> "123.45-6".replace(".", "").replace("-", "")
'123456'
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • 1
    Or to perform the replacement as a single pass, make a translation table up front (outside the function so you don't rebuild it over and over): `remove_dot_hyphen = str.maketrans("", "", ".-")`, then on each use, `"123.45-6".translate(remove_dot_hyphen)`. No real performance difference for short strings with only a couple characters to delete, but for longer strings, or for deleting more than two characters, `str.translate` is often faster. – ShadowRanger Mar 25 '19 at 15:13
0

lists don't have the join method, strings do

you could use this workaround: (if you're keen on using split and join)

list = cep.split(".")

cepnovo = "".join(list)

always check the documentation about which methods are provided on each data type... they may be different from language to language

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
danv
  • 87
  • 8
0

The exception you obtain is selfdescribing. Try this line:

cepnovo= "".join(cep.split("."))

If you want just to get the numeric symbols, instead of removing other symbols you can try finding the numeric ones

import re

text = "4332.24324.blablabl.45353-fewfe-32232"
print("".join(re.findall(r"\d+", text)))
Jaroslaw Matlak
  • 574
  • 1
  • 12
  • 23
0

You can try list comprehension and join

str = '123.345.322.22-00'
result = ''.join([x for x in str if x not in ['.', '-']])
Chia N.
  • 1
  • 1
-1

This sounds like a job for regex, if you are for sure parsing a string. Use it by import re and this should get rid of all the dots and dashes and replace them with nothing.

new_cep = re.sub('[.-]', '', cep)
TheN00bBuilder
  • 94
  • 1
  • 11
  • Not the down-voter, but getting regexes involved for something this simple is overkill (and *much* slower than simple `str.replace` or `str.translate` equivalents). It's also risky for folks who don't know regex well, as adding a new character after the hyphen would *dramatically* change behavior. – ShadowRanger Mar 25 '19 at 15:16