본문 바로가기
python

python single line to multi line

by kyeongseo.oh 2023. 1. 18.
smtp error 500 line too long이 발생함.
image를 encoding해서 보낼 때 line이 너무 길어서 발생한 문제로 multi line으로 변환해서 해결함.
step = 10
a = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
text = [a[i:i+step] for i in range(0, len(a), step)]
text = "\n".join(text)

print(text)

[output]
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaa

for i in range(0, len(a), step)를 통해 i에 0, 10, 20, 30, 40, 50, 60이 할당된다.

따라서 text 변수에는 [a[0:10], a[10:20], a[20:30], a[30:40], a[40:50], a[50:60], a[60:70]]이 할당되는 데 
["aaaaaaaaaa", "aaaaaaaaaa",aaaaaaaaaa",...] 이런 느낌이라고 보면 된다.

'python' 카테고리의 다른 글

이것저것  (0) 2023.01.27
report 생성  (0) 2023.01.23
aiosmtpd  (1) 2022.12.29
python fake data (mimesis)  (0) 2022.09.10
python fake data with multiprocess (Faker)  (0) 2022.09.09

댓글