简体   繁体   中英

Python Split on Literal Escape Character Text

I'm trying to split a string on an escape character. This api I'm dealing with simple returns a text value with "\\n" and "\\t" literally in the response (there is no actual tab character). I'm trying to split the response on the literal text matching the "\\t". An example response is below:

'1234567821\tcompany-fun'

How can I split this on the literal "\\t". When I do a string.split("\\t") that doesn't work as it's looking for an actual tab character. When I try string.split("\\\\t") in an attempt to escape the "\\" it also doesn't work.

Please let me know how to search for the actual text of an escape character. Thank you!

UPDATE: So in reality this has more to do with assignment and the apparent inability to pass a pointer in Python. The whole script looks like:

with open('orgs_ids.txt') as f:
orgs = f.read().splitlines()

formattedOrgs = [None]*len(orgs)

for i, org in enumerate(orgs):
    formattedOrgs[i] = org.split("\t")

I could not refer to orgs[i] and change the actually value (re-write it) I had to initialize a list of the same size and fill it in piece by piece. LMK if I'm thinking about this the right way. What I wanted to do is just rewrite orgs[i] into a org.split("\\t"), but I was not able to do that.

这是通过更广泛地看待问题来“解决”的:)

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