python (3) 썸네일형 리스트형 [python] 키-값 쌍으로 된 파일 읽어오기 json 파일 읽어오기 파일이 json 파일인 경우, built-in json 모듈을 사용한다. json.load()를 사용해서 파이썬 dioctionary로 불러오고 dictionary의 item() 메소드를 이용해서 키-값 쌍을 액세스한다. import json # Open the file containing key-value pairs with open('my_file.json') as f: data = json.load(f) # Access the key-value pairs for key, value in data.items(): print(key, value) csv 파일 읽어오기 키-값 쌍이 콤마로 분리된 경우 csv 모듈 사용 csv.reader()로 csv 파일을 읽어온 후.. [python] 파일에 텍스트 추가 기존 파일에 텍스트를 추가하려면 `open()` 함수에 a(append mode)를 사용한다. # Specify the file path and name file\_path = 'path/to/your/file.txt' # Check if the file exists if os.path.isfile(file\_path): # Open the file in append mode with open(file\_path, 'a') as f: # Write the text to the end of the file f.write('Hello, world!\\n') else: # If the file does not exist, create a new file and wri.. [python] set으로 구성된 dictionary에 있는 값들 중 같은 값을 추리기 # create a dictionary with sets as values my_dict = {'set1': {1, 2, 3}, 'set2': {2, 3, 4}, 'set3': {3, 4, 5}} # get the intersection of all sets in the dictionary intersection = set.intersection(*my_dict.values()) print(intersection) # output: {3} 이전 1 다음