전체 글 (12) 썸네일형 리스트형 [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} [linux] sed로 빈줄 제거 sed : 스트림 에디터 sed '/^\\s\*$/d' 명령행 분해 sed: stream editor 명령행, 텍스트를 파싱하고 변경한다. '/^\s*$/d': sed가 수행하는 명령 /^\s*$/: 레귤러 익스프레션으로 블랭크 라인을 매치한다. ^: 라인의 시작을 알림 \s*: 공백 캐릭터를 매치(spaces or tabs), including none. $: 라인의 끝 d: sed가 레귤러 익스프레션을 수행하여 매치한 라인을 지우라고 시킴 그래서 이 명령에 텍스트 입력을 수행하면 빈줄이 지워진 텍스트를 출력한다. 예_ example.txt 파일이 빈줄을 가졌을 때 이를 지우려면 다음과 같이 명령. 이것은 터미널에 결과를 출력한다. sed '/^\s*$/d' .. [linux] wget 파일 다운로드 ### wget -P : 다운로드 파일이 저장될 디렉토리를 지정하는데 쓰이는 옵션 ```bash wget -P /path/to/directory URL ``` 예) http://example.com/file.zip 를 다운받아 /home/user/downloads 에 저장하는 경우 ```bash wget -P /home/user/downloads http://example.com/file.zip ``` 환경변수 환경변수 추가 현재 세션에서만 유용하게 임시로 설정 export command 사용 export VARIABLE_NAME=value 예) MY_VAR 환경변수에 'HelloWorld'를 셋팅 export MY_VAR=HelloWorld 확인 echo $MY_VAR 영구적인 세팅 환경변수를 영구적으로 추가하려면 configuration files ~/.bashrc, ~/.profile or ~/.bash_profile에 추가한다. open shell profile file nano ~/.bashrc 파일 맨 밑에 'export' command를 추가한다. bash export MY\_VAR=HelloWorld 저장후 나와서 실행 source ~/.bashrc ## 폴더를 PATH environment va.. [nc] NetCat: Versatile Network Utility for TCP/UDP nc, or Netcat, is a versatile networking utility for reading from and writing to network connections using TCP or UDP protocols. It's often dubbed the "Swiss Army knife" of networking due to its wide range of capabilities. Netcat is commonly used for a variety of tasks related to networking diagnostics, development, and general purpose network communication. Key Features of Netcat: Port Scan.. [linux] 파일 비교 방법 1. diff diff 명령이 일반적 기본 사용법:diff file1.txt file2.txt 출력: 두 파일의 차이를 출력. 첫번째 파일을 두번째 파일과 같게 하기 위한 정보도 제공 Options: -y or --side-by-side: 하나씩 출력 -i: 대소문자 구분 무시 -w: 공백 무시 --color: 출력을 컬러로 표시 2. cmp The cmp command is simpler and faster than diff when you just want to check if the files are identical or where they first differ. Basic Usage:cmp file1.txt file2.txt Output: 기본으로, 두 파일이 다른 곳을 먼저 출력하고 만일 두.. 이전 1 2 다음