简体   繁体   中英

python strip function on special characters

i have a file like the one below. I am trying to read it, remove the slashes. The reading works file but when i remove slashes \\

module pe (c_imm_z03a, c_lt_func_z40a, c_or_z43a, c_opr1_z43a, 
    c_oprk_z43a, c_oprl_z43a, c_orm_z43a, c_wtn_z404a, 
    c_wtx_z404a, \imm_pkt_z43a[0] , \imm_pkt_z43a[1] , ldimm_z402b_b, 
    lt_anden_z43a, \lt_imm_z43a_b[0] , \lt_imm_z43a_b[1] , 
    \lt_imm_z43a_b[2] , \lt_imm_z43a_b[3] , \lt_imm_z43a_b[4] , 
    \lt_imm_z43a_b[5] , \lt_imm_z43a_b[6] , \lt_imm_z43a_b[7] , 
    \lt_imm_z43a_b[8] , \lt_imm_z43a_b[9] , \lt_imm_z43a_b[10] ,


lines=f.readlines()
for line in lines:
  if "\\" in line:
    print line.strip("\\")

This doesnt get me what i want, and i am not sure why not. How do i fix it? Help is appreciated !

str.strip() removes characters from the start and end of a string only . Quoting the documentation :

Return a copy of the string with the leading and trailing characters removed.

Emphasis mine.

To remove slashes throughout a line, use str.replace() :

print line.replace("\\", '')

or use str.translate() for a faster approach to delete characters:

print line.translate(None, '\\')

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