In Python: how can I say: if line.partition ('#' or 'tab') ... do something

In Python: how can I say:

line = line.partition('#' or 'tab')[0]   ... do something with 

I know that I can:

line = line.partition('#')[0]  ... do something

But what is the code for the tab character, and can I say # or tab?

Update: I am trying to read the first word on each line if you read #, and then ignore everything after this character (since this is a comment). But then I found that if I had # in the first tab of the word, then it will read the tab as part of the first word. So I tried to say if you read a tab or hash and then treat the line as a comment. The work around is simply to put a space after the first word, and not on the tab. But it is not very elegant. Now I understand that the if statement was wrong, I tried to simplify the situation too much. The above is already true, but I think the path of Ned Batchelder is the path, but maybe now there is something else that you know what I'm trying to do.

+3
4

# , .

raw_data, _, _ = line.partition("#")
data= raw_data.strip()
if len(data) == 0:
    continue # or whatever, the data part of the line is empty
# you have data 

, .

[raw_data, _, _ = line.partition("#") "#" raw_data, "#" _. "#" _. _, , .]

data, _, _ = line.strip().partition("#")

, .

+2

, re.split:

re.split("(#|\t)", line, 1)

re.split , parens, , maxsplit ( 1). , .

, , , ...

+10

'\t' - , .

import re

match = re.search('[#\t]', line)
if match:
    i, j = match.span()
    return (line[:i], line[i:j], line[j:])
return (line, '', '')

, : (head, sep, tail).

+2

#, '\ t'.

( , ):

first_word, space, rest = Line.partition('\ t')

first_word split \t (tab). , - #. :

first_word_2, s_2, r_2 = first_word.partition('#')

first_word_2 , .

S

0

Source: https://habr.com/ru/post/1722528/


All Articles