파이썬은 긴 문자열을 자릅니다


246

파이썬에서 문자열을 75 자로 자르는 방법은 무엇입니까?

이것이 JavaScript에서 수행되는 방법입니다.

var data="saddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddsaddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddsadddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
var info = (data.length > 75) ? data.substring[0,75] + '..' : data;

답변:


427
info = (data[:75] + '..') if len(data) > 75 else data

58
len(data) > 77이중 점을 설명하기 위해 조건을 변경 하려고합니다 (마지막 문자 만 점으로 자르면 끝이 잘리지 않습니다).
하센

5
@ hasenj : 원래 코드와 일치하지 않지만 처음부터 지적해야 할 좋은 제안입니다.
Marcelo Cantos

2
포함 된 parens는 물론 선택 사항입니다.
Taylor Edmiston

10
@TaylorEdmiston True,하지만 매일 사용하는 5-10 개 언어의 모든 우선 순위 규칙을 기억하지 못하는 사람들에게 매우 유용합니다.
Marcelo Cantos

2
@Anthony a slice
Marcelo Cantos

126

더 짧은 :

info = data[:75] + (data[75:] and '..')

2
그것을하는 재미있는 방법. 여전히 복합 원 라이너입니다. ^^
Cheery

3
'..'을 포함하면이 솔루션에 77자를 사용할 수 없습니까?
Mark Chackerian

이것이 두 개의 슬라이스 작업을 수행하지 않습니까? 성능이 중요 할 때 stackoverflow.com/a/52279347/1834057 과 비교하여 이것이 어떻게 수행되는지 궁금합니다.
Nicholas Hamilton

1
물론 독창적 인 대답이지만 Marcelo의 대답은 더 명확하고 읽기 쉽기 때문에 더 좋습니다 (따라서 Pythonic).
sitnarf

114

더 간결한 :

data = data[:75]

75 자 미만이면 변경되지 않습니다.


9
아마도 문자열이 잘 리면 줄임표가 추가되기를 원할 것입니다.
FogleBird

4
당신 말이 맞아요. 다른 답변보다 더 나은 방법을 생각할 수 없습니다.
neil

82

Python 3.4+를 사용 textwrap.shorten하는 경우 표준 라이브러리에서 사용할 수 있습니다 .

주어진 너비에 맞게 주어진 텍스트를 축소하고 자릅니다.

먼저 텍스트의 공백이 축소됩니다 (모든 공백이 단일 공백으로 바)). 결과가 너비에 맞는 경우 반환됩니다. 그렇지 않으면 나머지 단어와 자리 표시자가 너비에 맞도록 끝에서 충분한 단어가 삭제됩니다.

>>> textwrap.shorten("Hello  world!", width=12)
'Hello world!'
>>> textwrap.shorten("Hello  world!", width=11)
'Hello [...]'
>>> textwrap.shorten("Hello world", width=10, placeholder="...")
'Hello...'

8
실제로 긴 줄 (공백 없음)에서 바지를 긁어 내고 줄임표 만 출력하는 것 같습니다.
elBradford

5
@elBradford (및 다른 사람들에게 관심이 있음) : 단일 문자가 아닌 단어를shorten() 자르기 때문 입니다. 검색했지만 단어가 아닌 단일 문자를 자르 도록 구성 하거나 인스턴스를 구성하는 방법이 없습니다. shorten()TextWrapper
Acsor

그리고 그것은 줄 바꿈을 제거함으로써 성가신 부작용이 있습니다
havlock

이것은 OP의 질문을 해결하지 못합니다. 단어별로 잘리고 공백을 제거합니다.
Florian Wendelborn



9

정규식으로 :

re.sub(r'^(.{75}).*$', '\g<1>...', data)

긴 문자열이 잘립니다.

>>> data="11111111112222222222333333333344444444445555555555666666666677777777778888888888"
>>> re.sub(r'^(.{75}).*$', '\g<1>...', data)
'111111111122222222223333333333444444444455555555556666666666777777777788888...'

더 짧은 문자열은 절대로 잘리지 않습니다.

>>> data="11111111112222222222333333"
>>> re.sub(r'^(.{75}).*$', '\g<1>...', data)
'11111111112222222222333333'

이런 식으로 문자열의 중간 부분을 "잘라낼"수도 있습니다.

re.sub(r'^(.{5}).*(.{5})$', '\g<1>...\g<2>', data)

>>> data="11111111112222222222333333333344444444445555555555666666666677777777778888888888"
>>> re.sub(r'^(.{5}).*(.{5})$', '\g<1>...\g<2>', data)
'11111...88888'

문자열에 공백이 있으면 효과가 없었습니다
holms

왜 그런 간단한 경우에 정규식을 사용 하시겠습니까?
Bora M. Alper

5

이 방법은 다음과 같은 경우를 사용하지 않습니다.

data[:75] + bool(data[75:]) * '..'


4
나는 그것이 가능하다는 것을 보여주기 위해서만 썼다. 파이썬의 가독성 철학에 위배됩니다. 다른 "if"기반 방법에 비해 성능상의 이점이 없습니다. 나는 그것을 사용하지 않으며 당신도 그것을 사용하지 않는 것이 좋습니다.
Sassan

4
limit = 75
info = data[:limit] + '..' * (len(data) > limit)

1
이것은 가장 우아한 솔루션입니다. 또한 75불일치를 피하기 위해 chars limit (이 경우 )을 변수로 추출합니다 . limit = 75; info = data[:limit] + '..' * (len(data) > limit)
ekauffmann

3

또 다른 해결책. 로 TrueFalse당신은 마지막 시험에 대한 약간의 피드백을 얻을.

data = {True: data[:75] + '..', False: data}[len(data) > 75]

2

이것은 단지 :

n = 8
s = '123'
print  s[:n-3] + (s[n-3:], '...')[len(s) > n]
s = '12345678'
print  s[:n-3] + (s[n-3:], '...')[len(s) > n]
s = '123456789'     
print  s[:n-3] + (s[n-3:], '...')[len(s) > n]
s = '123456789012345'
print  s[:n-3] + (s[n-3:], '...')[len(s) > n]

123
12345678
12345...
12345...

이전의 모든 대답은 OP가 실제로 원하는 것을 고려하지 않습니다. 출력 문자열은 75자를 넘지 않아야합니다. "내가 말한 것을하지 말고, 내가 원하는 것을한다"프로그래밍 원리를 이해하기위한 전문가. 완전성을 위해 다음을 추가하여 n <3의 코너 케이스를 수정할 수 있습니다. if n> 2 else s [: n]
Dave

1
       >>> info = lambda data: len(data)>10 and data[:10]+'...' or data
       >>> info('sdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdf')
           'sdfsdfsdfs...'
       >>> info('sdfsdf')
           'sdfsdf'
       >>> 

1
답을 설명해주세요.
Gwenc37

이 함수의 비슷한 예 def info2 (data) : if len (data)> 10 : return data [: 10] + '...'else : 함수형 스타일로 이름없는 디자인의 데이터 람다 명령어를 반환합니다. ex = lambda x : x + 1 def ex (x) : return x + 1
Spouk

1

동적으로 할당 된 C 문자열을 수행 할 수있는 것처럼 실제로 파이썬 문자열을 "잘라낼 수"없습니다. 파이썬의 문자열은 변경할 수 없습니다. 당신이 할 수있는 일은 다른 답변에서 설명한대로 문자열을 슬라이스하여 슬라이스 오프셋과 단계로 정의 된 문자 만 포함하는 새 문자열을 생성하는 것입니다. 실용적이지 않은 일부 경우에는 인터뷰 언어로 Python을 선택하고 면접관이 문자열에서 중복 문자를 제자리에서 제거하도록 요청할 때와 같이 약간 성 가실 수 있습니다. 도


1
info = data[:min(len(data), 75)

코드 전용 답변은 일반적으로 품질이 낮은 것으로 간주됩니다. 답변에 설명을 추가해 주시겠습니까?
레몬 카지

0

정규 표현식이 필요하지 않지만 허용되는 답변에 문자열 연결 대신 문자열 형식을 사용하고 싶습니다.

이것은 아마도 문자열 data을 75 자로 자르는 가장 정식적인 Pythonic 방법 일 것입니다 .

>>> data = "saddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddsaddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddsadddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
>>> info = "{}..".format(data[:75]) if len(data) > 75 else data
>>> info
'111111111122222222223333333333444444444455555555556666666666777777777788888...'

나는 당신의 saddddddd...문자열이 어떻게 변하는 지 재미 111111...있다는 것을 알았습니다.
akarilimano

0

여기에 새로운 String 클래스의 일부로 만든 함수가 있습니다 ... 접미사를 추가 할 수 있습니다 (문자열이 자르고 크기가 길어지면 길이가 길다면 절대 크기를 강요 할 필요는 없지만)

나는 몇 가지 사항을 바꾸는 과정에 있었고 더 이상 필요하지 않고 쓸데없는 논리 비용 (예 : _truncate ... 등)이 있습니다.

그러나 여전히 데이터를 자르는 데 유용한 기능입니다 ...

##
## Truncate characters of a string after _len'nth char, if necessary... If _len is less than 0, don't truncate anything... Note: If you attach a suffix, and you enable absolute max length then the suffix length is subtracted from max length... Note: If the suffix length is longer than the output then no suffix is used...
##
## Usage: Where _text = 'Testing', _width = 4
##      _data = String.Truncate( _text, _width )                        == Test
##      _data = String.Truncate( _text, _width, '..', True )            == Te..
##
## Equivalent Alternates: Where _text = 'Testing', _width = 4
##      _data = String.SubStr( _text, 0, _width )                       == Test
##      _data = _text[  : _width ]                                      == Test
##      _data = ( _text )[  : _width ]                                  == Test
##
def Truncate( _text, _max_len = -1, _suffix = False, _absolute_max_len = True ):
    ## Length of the string we are considering for truncation
    _len            = len( _text )

    ## Whether or not we have to truncate
    _truncate       = ( False, True )[ _len > _max_len ]

    ## Note: If we don't need to truncate, there's no point in proceeding...
    if ( not _truncate ):
        return _text

    ## The suffix in string form
    _suffix_str     = ( '',  str( _suffix ) )[ _truncate and _suffix != False ]

    ## The suffix length
    _len_suffix     = len( _suffix_str )

    ## Whether or not we add the suffix
    _add_suffix     = ( False, True )[ _truncate and _suffix != False and _max_len > _len_suffix ]

    ## Suffix Offset
    _suffix_offset = _max_len - _len_suffix
    _suffix_offset  = ( _max_len, _suffix_offset )[ _add_suffix and _absolute_max_len != False and _suffix_offset > 0 ]

    ## The truncate point.... If not necessary, then length of string.. If necessary then the max length with or without subtracting the suffix length... Note: It may be easier ( less logic cost ) to simply add the suffix to the calculated point, then truncate - if point is negative then the suffix will be destroyed anyway.
    ## If we don't need to truncate, then the length is the length of the string.. If we do need to truncate, then the length depends on whether we add the suffix and offset the length of the suffix or not...
    _len_truncate   = ( _len, _max_len )[ _truncate ]
    _len_truncate   = ( _len_truncate, _max_len )[ _len_truncate <= _max_len ]

    ## If we add the suffix, add it... Suffix won't be added if the suffix is the same length as the text being output...
    if ( _add_suffix ):
        _text = _text[ 0 : _suffix_offset ] + _suffix_str + _text[ _suffix_offset: ]

    ## Return the text after truncating...
    return _text[ : _len_truncate ]

1
모든 단일 인수와 변수에서 모든 밑줄은 무엇입니까?
Nicholas Hamilton

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.