4

I am a beginner in Python programming and I have a question about constructing a code.

Let's say I have the following data:

150 z   Brazil
160 a   Toys R Us

I want to code such that if we see the pattern bbb \t (one digit or character not a) \t, I would replace the bbb \t digit \t with bbb_$d$_. (Here, \t indicates tab and bbb indicates a number).

Thus, the output would be 150_$z$_Brazil, and the output for 160 a Toys R Us would not be obtained as the digit after 160 is a.

My question is, how do I code such that I select one digit or character that is not a?

2
  • Text pattern matching like this is often done with regular expressions. Regular expressions can get (very) complicated, but they can be useful even if you just learn a few of the basics. Sometimes if your data is more structured, you can get by with simpler things like mytext.split('\t') Commented Jun 9, 2015 at 3:22
  • @Marius: Doesn't regex require the output to be in list form? I saw the link and watched some tutorials on regex but seems like the output is in a form of a list. Commented Jun 9, 2015 at 3:56

1 Answer 1

5

Q. how do I code such that I select one digit or character that is not a?

A. '[^a]'

The regular expression syntax is documented here.

Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't regex require the output to be in list form? I saw the link and watched some tutorials on regex but seems like the output is in a form of a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.