이 글은 파이썬에서 고정 길이에 따라 문자열을 분할하는 방법을 주로 소개합니다. 이제 이를 공유합니다. 도움이 필요한 친구들이 참고할 수 있습니다. 특정 값으로 변경해야 합니다. 예를 들어 mac='902B345FB021'은 mac='90-2B-34-5F-B0-21'로 변경됩니다.
파이썬 스크립트를 사용하면 쉽게 구현할 수 있습니다. 원칙은 문자열이 고정된 길이에 따라 분할된다는 것입니다.
1, 파일 mac.txt는 다음 mac 주소를 저장합니다:
902B3413EFA6방법 1:50E549ECBA1C
다음은 모두가 공유할 수 있는 두 가지 구현 방법입니다. 참조하십시오.
902B3457B16F
1C6F65296DF9
902B34 131A14
50E549E3E2F8
50E5493A2696
902B345FB021
902B34131574
코드 샘플:
#!/bin/python #site:WWW.jb51.net # A = open('mac.txt','r') a = A.readlines() for aa in a: b = list(aa.strip()) c='' for i in range(len(b)): if i !=0: if i%2 == 0: c=c+'-'+b[i] else: c=c+b[i] else: c=c+b[i] print c A.close()
방법 2:
코드 샘플:#!/bin/python # import re A = open('mac.txt','r') a = A.readlines() for aa in a: b=re.findall(r'.{2}',aa) c='-'.join(b) print c A.close()
파이썬의 정규식 구현을 사용하여 실행 효율성이 높으며 추천할만한 가치가 있습니다.
처리 결과:90-2B-34-13-EF-A6Python은 문자열을 고정 길이에 따라 세 문자 그룹으로 나눕니다.50-E5-49-EC-BA-1C
Python은 여전히 문자열 처리에 매우 능숙합니다. 모두가 확실히 이해하는 것이 좋습니다.
90-2B-34-57 -B1-6F
1C-6F-65-29-6D-F9
90-2B-34-13-1A-14
50-E5-49-E3-E2-F8
50-E5-49-3A-26 -96
90-2B-34-5F-B0-21
90-2B-34-13-15-74
90-2B-34-18-43-BF
00-24-1D-0E-25-8D
def cut_text(text,lenth): textArr = re.findall('.{'+str(lenth)+'}', text) textArr.append(text[(len(textArr)*lenth):]) return textArr print(cut_text('123456789abcdefg',3)) ['123', '456', '789', 'abc', 'def', 'g']
Code 2
>>> import re >>> string = '123456789abcdefg' >>> re.findall(r'.{3}', string) ['123', '456', '789', 'abc', 'def'] >>>
관련 권장 사항:
python 인쇄 쉼표나 공백으로 구분하는 방법 Python은 문자열의 지정된 위치에 문자를 삽입하는 기능을 구현합니다위 내용은 고정 길이에 따라 문자열을 분할하는 Python 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

PythonlistsCanstoreAnyDatAtype, ArrayModuLearRaysStoreOneType 및 NUMPYARRAYSAREFORNUMERICALPUTATION.1) LISTSAREVERSATILEBUTLESSMEMORY-EFFICENT.2) ARRAYMODUERRAYRAYRAYSARRYSARESARESARESARESARESARESAREDOREDORY-UNFICEDONOUNEOUSDATA.3) NumpyArraysUraysOrcepperperperperperperperperperperperperperperperferperferperferferpercient

whenyouattempttoreavalueofthewrongdatatypeinapythonaphonarray, thisiSdueTotheArrayModule의 stricttyPeenforcement, theAllElementStobeofthesAmetypecified bythetypecode.forperformancersassion, arraysaremoreficats the thraysaremoreficats thetheperfication the thraysaremorefications는

Pythonlistsarepartoftsandardlardlibrary, whileraysarenot.listsarebuilt-in, 다재다능하고, 수집 할 수있는 반면, arraysarreprovidedByTearRaymoduledlesscommonlyusedDuetolimitedFunctionality.

thescriptIsrunningwithHongpyThonversionDueCorRectDefaultTerpretersEttings.tofixThis : 1) checktheDefaultPyThonVersionUsingPyThon-VersionorPyThon3- version.2) usvirtual-ErondmentsBythePython.9-Mvenvmyenv, 활성화, 및 파괴

PythonArraysSupportVariousOperations : 1) SlicingExtractsSubsets, 2) 추가/확장 어드먼트, 3) 삽입 값 삽입 ATSpecificPositions, 4) retingdeletesElements, 5) 분류/ReversingChangesOrder 및 6) ListsompectionScreateNewListSbasedOnsistin

NumpyArraysareSentialplosplicationSefficationSefficientNumericalcomputationsanddatamanipulation. Theyarcrucialindatascience, MachineLearning, Physics, Engineering 및 Financeduetotheiribility에 대한 handlarge-scaledataefficivally. forexample, Infinancialanyaly

UseanArray.ArrayOveralistInpyThonWhendealingwithhomogeneousData, Performance-CriticalCode, OrinterFacingwithCcode.1) HomogeneousData : ArraysSaveMemorywithtypepletement.2) Performance-CriticalCode : arraysofferbetterporcomanceFornumericalOperations.3) Interf

아니요, NOTALLLISTOPERATIONARESUPPORTEDBYARRARES, andVICEVERSA.1) ArraySDONOTSUPPORTDYNAMICOPERATIONSLIKEPENDORINSERTWITHUTRESIGING, WHITHIMPACTSPERFORMANCE.2) ListSDONOTEECONSTANTTIMECOMPLEXITEFORDITITICCESSLIKEARRAYSDO.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

안전한 시험 브라우저
안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

에디트플러스 중국어 크랙 버전
작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음
