• Home
  • About
    • lahuman photo

      lahuman

      열심히 사는 아저씨

    • Learn More
    • Facebook
    • LinkedIn
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

Python] string 을 json.loads로 객체화 할때 유의점

26 Feb 2018

Reading time ~1 minute

Python] string 을 json.loads로 객체화 할때 유의점

문자열 JSON 형식을 파일로 저장 하고, 해당 파일을 다시 객체화 할때 다음과 같이 사용한다.

# File a.json
{
	"a":"A",
	"b":"\"B\"
} 
# Phton Code
with open('a.json') as f:
	data = json.loads(str(f.read()).encode("string-escape").replace('\\\\"', '\\"'))
	... # 처리

마지막에 .replace(‘\\”’, ‘\”’) 처리를 하지 않으면, ValueError: Expecting , delimiter: line 1 column … 같은 오류가 발생한다.



PythonTIP Share Tweet +1