Python(14)
-
TypeError: a bytes-like object is required, not 'str'
데이터가 byte이면, 해당 에러 발생 output = [b' 0: 8514fix\r\n', b' 1: 8514oem\r\n', b' 2: Ami R\r\n', b' 3: Arial\r\n'] for data in output : font = data.split(':')[1].strip() print(font) byte 데이터에 decode()를 해줘야 해결 됨 output = [b' 0: 8514fix\r\n', b' 1: 8514oem\r\n', b' 2: Ami R\r\n', b' 3: Arial\r\n'] for data in output : font = data.decode().split(':')[1].strip() print(font) 반대는 encode() 해야 함
2021.03.28 -
정규식 사용하기
데이터 형식 : 592/? ((9003,4707),(9012,4719)) import re p = re.search(' (\d+)/(.) \(\((\d+),(\d+)\),\((\d+),(\d+)',line) print(p.groups()) boxData = p.groups() 데이터 형식 : /aaa/bbb/99 max = '/aaa/bbb/99' max = re.search('/(\d+)$',max) print(max) print(max.groups()) print(max.group(0)) print(max.group(1)) 결과 ('99',) /99 99
2021.03.13 -
파일이 존재하면, 마지막 라인을 읽어오기
if os.path.isfile(boxFile) : with open(boxFile, 'r', encoding='utf8') as f : lines = f.readlines() data = lines[len(lines)-1].split(' ') x = int(data[1]) y = imageHeight - int(data[4]) width = int(data[3]) - x height = imageHeight - y - int(data[2]) x += width + space newImage = pil.Image.open(imageFile)
2021.03.13 -
파이썬 객체 타입 확인
print(type(queryset))
2021.02.07 -
shlex 문자열안의 따옴표 구분하기
import shlex temp = "abc 'afasfasfs aaaa' dgg" print(shlex.split(temp) > ['abc', 'afasfasfs aaaa', 'dgg']
2020.12.31 -
2차배열 정렬
from operator import itemgetter results = [ [1, 3, 5], [2, 5, 8], [3, 1, 2], ] results.sort(key=itemgetter(1)) print(results) [ [3, 1, 2], [1, 3, 5], [2, 5, 8] ]
2020.12.27