简体   繁体   中英

Stripping Out Characters

I have this code writing to a file;

rline = quantity," x ",product, productcost

When I read the file, I get this (8, ' x ', '98456390,Paper Tray,6\\r\\n', 48) I am trying to strip out the characters - brackets, speech marks and\\r\\n

I am trying to use rline.strip, but I get AttributeError: 'tuple' object has no attribute 'strip'

Does anybody know how I can get around this?

Technically, there aren't any brackets or quotes.

The following line of code creates a tuple consisting of quantity , the string " x " (ie an x surrounded by a space on each side), product , and productcost .

rline = quantity," x ",product, productcost

The parantheses and quotes are a result of representing the entire tuple as a string.

First, you want to convert this to a string by combining all of the elements:

joined_string = ''.join(map(str, rline))

Next, you want to remove the return characters, \\r\\n :

joined_string.replace('\r\n', '')

All of that combined:

stripped_string = ''.join(map(str, rline)).replace('\r\n', '')

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