파일 열기 (open, read)
<Script>
=> file = open('data.txt')
content = file.read()
file.close()
=> with open('data.txt') as file:
content = file.read()
---
contents = []
with open('data.txt') as file:
for line in file:
content.append(line)
---
with open('data.txt', 'w') as file:
file.write('Hello')
</Script>
데이터 관리
<Script>
list.strip()
=> 각 줄의 앞-뒤의 모든 공백이 사라짐(공백 문자)
dict_list = {}
with open (filename) as file:
for line in file:
user, title = line.strip().split(':')
dict_list[user] = title
return dict_list
</Script>
JSON
<Script>
import json
with open(filename) as file:
json_string = file.read()
return json.loads(json.string)
with open(filename, 'w') as file:
json_string = json.dumps(dictionary)
file.write(json_string)
</Script>