본문 바로가기
파이썬

파이썬 Python - converting textfile contents into dictionary values/keys easily

by º기록 2020. 10. 3.
반응형

다음과 같은 텍스트 파일이 있다고 가정 해 보겠습니다.

line = "this is line 1"
line2 = "this is the second line"
line3 = "here is another line"
line4 = "yet another line!"

그리고 "line *"이 키이고 따옴표로 묶인 텍스트를 값으로 사용하는 동시에 등호를 제거하는 사전 키 / 값으로 빠르게 변환하고 싶습니다.

파이썬에서 이것을 수행하는 가장 좋은 방법은 무엇입니까?

 

해결 방법

 

f = open(filepath, 'r')
answer = {}
for line in f:
    k, v = line.strip().split('=')
    answer[k.strip()] = v.strip()

f.close()

도움이 되었기를 바랍니다

 

참조 페이지 https://stackoverflow.com/questions/5942874

 

 

반응형

댓글