파이썬
파이썬 readlines 결과에서 \n 제거하기 / read().splitlines() 사용하기
mcdn
2023. 4. 6. 13:22
반응형
파이썬 readlines()하고
파이썬 readlines()를 실행했는데
이렇게 생긴 input.txt 를
개행문자까지 붙여서 돌려주고 있다.
여기서 깔끔하게 \n 없이 출력하고 싶은데 그 방법은
readlines()대신 read().splitlines()
def main():
f = open('./input.txt', 'r')
lines = f.read().splitlines()
f.close()
print(lines)
main()
def main():
f = open('./input.txt', 'r')
lines = f.read().splitlines()
f.close()
print(lines)
main()
read().splitlines()를 사용하는 것이다.
그러면 \n 개행문자 없이 깔끔하게 split된 문장이 나온다.
참고 :
https://stackoverflow.com/questions/15233340/getting-rid-of-n-when-using-readlines
참고로 만약 에러가 난다면
AttributeError: 'builtin_function_or_method' object has no attribute 'splitlines'
위와 같은 오류가 난다면
위와 같이 read 내장함수를 () 없이 사용하는 것은 아닌지 확인해보자.
이게 올바른 사용방법이다.
반응형