Input
- input() 으로 해결
1
2
3
4
5
data = input()
print(data)
data = input("only numeric\n")
print(type(data))
print(data)
1
2
3
4
5
6
asdf
asdf
only numeric
1234
<class 'str'>
1234
- input으로 받는데
input에 필요한 문구를 넣을 수 있음 - input에 뭐가 들어가던
string로 받을 수 있음
Output
- 출력은
print()로 한다. 지금까지 뭘 하던 결과볼때 쓰던건데
안에 출력하고 싶은걸 쓰는데
출력은 생각보다 알아서 잘 해준다.
예를들어1 2 3 4
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; Console.WriteLine(numbers); // System.Collections.Generic.List`1[System.Int32]
1 2 3 4
numbers = [1, 2, 3, 4, 5] print(numbers) # [1, 2, 3, 4, 5]
- 이런경우 특히.
이거 외에도 저런 구조회된 데이터를 볼 때
별다른 작업 없이도 알아서 해준것같음.
- 이런경우 특히.
출력할 parameter가 둘 이상인 경우
몇가지 방법이 있는데
,,+, 이어붙이기 정도1 2 3 4 5 6
b = "str" c = 2 print(b + "문자, 수" + str(c)) print(b, "문자, 수", c) print("basdf""text")
1 2 3
str문자, 수2 str 문자, 수 2 basdftext
+의 경우
string만 됨.,의 경우
그냥 나열하는 느낌?
type는 상관없음." "를 나열하는 경우는
" "끼리만 됨
sep, end, …
- 지금까지 출력할 때 쓴 print는
,로 나열한 경우
실제 출력에서' '(whitespace)가
하나씩 붙어있는게 보인다.- 또, print는 무조건 한줄을 사용한다.
C#은Console.WriteLine();과
Console.Write();
이 다른데 출력라인 끝에\n의 유뮤 차이.
print에서 sep, end를 명시해
이런 출력을 다르게 할 수 있음1 2 3 4 5
print("str1", "str2", "str3") print("str1", "str2", "str3", sep=" ! ") print("str1", "str2", "str3", end="") print("str1", "str2", "str3", end="^^")
1 2 3
str1 str2 str3 str1 ! str2 ! str3 str1 str2 str3str1 str2 str3^^
- sep는 string끼리 연결 관련,
end는 print 끝 관련.
- sep는 string끼리 연결 관련,
stdxxx
1 2 3
import sys print("str1", "str2", "str3", file=sys.stderr) print("str1", "str2", "str3", file=sys.stdout)
1 2
str1 str2 str3 str1 str2 str3
- 몰?루
- stdout는 걍 출력, stderr은 에러.
- 정확치는 않은데 log level같은 개념인것같음.
- 아무튼, 이게 들어는 위치가
file=하고 쓰는데
여기는 파일 출력할때 씀.
이건 뒤에서.
print 툴팁을 다시 보면
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)이런걸 볼 수 있는데
sep, end, file, flush로
어지간한 출력은 다 끝내는듯하다.
출력포맷
1
2
3
4
5
6
7
8
9
10
11
12
13
# 10개 자릿수, 빈공간, 오른정렬
print("{0: >10}".format(50000000000000))
print("{0: >10}".format(500))
# 10개 자릿수, 빈공간에 문자, 왼정렬
print("{0:*<10}".format(500))
# 음,양 표시
print("{0: >+10}".format(500))
print("{0: >-10}".format(-500))
# 소수점 자리수
print("{0: >10.2f}".format(500.486260))
1
2
3
4
5
6
50000000000000
500
500*******
+500
-500
500.49
- 전 포스팅에서 string - format 내용 중
{}.format()에 변경하는 방식인데
{[arg]:[empty str][sort][+-][num len][num format]} - 위부터 내려오면서 보면 알겠지만,
하나하나 조합하면 계속 더 복잡하게 만들 수 있음.
"{0:-<+20.4f,}"이런식으로.. - 소수점은 다음 자리에서 반올림.
위 같은 경우 3자리서 반올림. - 글자 자릿수같은 경우 넘으면 그냥 출력해주고
자릿수를 충족하지 못하는경우의 조건인듯하다.
파일 입출력
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
test = open("test.txt", "w", encoding="utf-8")
test.write("Hello World")
test.close()
test = open("test.txt", "a", encoding="utf-8")
test.write("\\nHello python")
test.close()
test = open("test.txt", "r", encoding="utf-8")
print(test.read())
test.close()
test = open("test.txt", "r", encoding="utf-8")
for line in test.readlines():
print(line)
test.close()
- 뭔가 간편해보임…….
- w,a,r 사용방법.
- w : write. 생성. 이미 있는경우에도 새로 생성하는듯.
- a : append. 아래 이어서 써줌. 파일이 없으면 생성.
- r : read. 읽음. 읽는 방법은 크게 3가지
- read() : 파일 전체를 str로 읽어옴.
- readLine() : 한 줄 읽어옴
while로 파일 끝까지 읽을 수 있음. - readLines() : 파일 전체를
list[str]로 읽어옴. - 위 세개 방법에 또 세부 옵션이 있음.
몇줄 읽기 등등.
pickle
- 데이터를 파일형태로 다루기 위해 사용
정도로 알고있었는데 - 객체 구조의 직렬화와 역 직렬화를 위한
바이너리 프로토콜을 구현합니다.
를 시작으로 설명이 좀 길게 나옴 - 다 빼고 간단하게 쓰는데
나머지는 필요하면 나중에 추가.
Write
1
2
3
4
5
6
7
import pickle
pickle_test1 = open('test.pkl', 'wb')
tempData1 = ['a', 'b', 'c']
pickle.dump(tempData1, pickle_test1)
pickle_test1.close()
- 동작에(wb) b가 꼭 붙어야함 binary래.
- 쓰는건 dump.
Read
1
2
3
4
5
pickle_test2 = open('test.pkl', 'rb')
tempData2 = pickle.load(pickle_test2)
print(tempData2)
pickle_test2.close()
- 위에서 쓴 pickle파일을 읽는방법.
- 여기도 동작에 b를 꼭 붙인다함.
- load로 읽어옴.
with
1
2
3
4
5
6
7
8
with open('test.pkl', 'wb') as pickle_test3:
tempData3 = ['d', 'e', 'f']
pickle.dump(tempData3, pickle_test3)
with open('test.pkl', 'rb') as pickle_test4:
tempData4 = pickle.load(pickle_test4)
print(tempData4)
- pickle에 쓴 예문을 with과 함께 다시 쓴것.
- using(~~)랑 비슷한듯.
- with 문단이 끝나면 close가 자동으로 된다함
이건 pickle가 아닌 일반 파일에도 적용됨.
Comments powered by Disqus.