# 콘솔파입출력: 리다이렉션 기능
#
a=[1,2,3,4,5,6]
for i in range(len(a)-1):
print(a[i],",",end="")
print(a[len(a)-1])
python a.py > a.txt
python a.py >> a.txt
#
#
#
a=[]
for i in range(3):
b = input()
a.append(b)
print(a)
#다음과 같이 키보드에서 입력
1
2
3
>>>['1', '2', '3']
----- a.txt -----
1
2
3
----------------
python a.py < a.txt
>>>['1', '2', '3']
#
# 파일에 출력
#
#
# 텍스트 파일 - 기본
#
----------------------------------------
file = open("test.txt", "w")
file.write('hello')
file.write("\n")
file.write('world')
file.write("\n")
file.close()
file=open("test.txt", "r")
lines = file.read()
file.close()
print(lines)
#
msg = ["HELLO\n", "WORLD\n", "안녕\n"]
file = open("test.txt", "w", encoding='utf-8')
file.writelines(msg)
file.close()
file = open("test.txt", "r", encoding='utf-8')
line = file.readline()
while line != '':
print(line, end='')
line = file.readline()
file.close()
print()
file = open("test.txt", "r", encoding='utf-8')
lines = file.readlines()
file.close()
line = ''
for line in lines:
print(line, end='')
----------------------------------------
리스트에 있는 값 저장
----------------------------------------
import csv
f=open('a.csv', 'w',newline='')
writer=csv.writer(f, delimiter=',')
writer.writerow([1,2,3])
writer.writerow([4,5,6])
f.close()
----a.csv----
1, 2, 3
4, 5, 6
-------------
import csv
f=open('a.csv', 'r', encoding='utf-8')
reader=csv.reader(f)
for line in reader:
print(line)
f.close()
>> 실행결과
['1', '2', '3']
['4', '5', '6']
----------------------------------------
data=[1,2,3]
with open('a.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(data)
----------------------------------------
import numpy as np
data = np.loadtxt('a.csv', delimiter=',', dtype=np.float32)
print(data)
>> 실행결과
[[1. 2. 3.]
[4. 5. 6.]]
----------------------------------------
f = open('a.csv', 'r')
lines = f.read()
print(lines)
f.close()
>> 실행결과
1,2,3
4,5,6
----------------------------------------
#
# 이진파일
#
import pickle
colors = [1, 2, 3, 4]
pickle.dump(colors, open('colors.txt', 'wb'))
mycolors = pickle.load(open('colors.txt', 'rb'))
print(mycolors)
----------------------------------------
#
# 원그리기 / 파이 추정(근사)하기
#
#
# 무식한 방법으로 근사하기 ~ 몬테카를로 방법
#
import random
import math
import csv
xset = []
yset = []
def generateSamples(size):
xset.clear()
yset.clear()
for i in range(size):
x = random.random()
y = random.random()
xset.append(x)
yset.append(y)
def isInsideCircle(x, y):
r = 0.5
distance = (x - 0.5)*(x - 0.5) + (y - 0.5)*(y - 0.5) - r*r
if (distance >= 0.0):
return False
else:
return True
def estimagePi():
area_square = 10000
area_circle = 0
generateSamples(area_square)
dataset=[0, 0]
with open('a.csv', 'w', newline='') as csvfile:
points = csv.writer(csvfile, delimiter=',')
for i in range(len(xset)):
x= xset[i]
y = yset[i]
if (isInsideCircle(x, y)==True):
dataset[0]=x
dataset[1]=y
'''points.writerow(dataset)'''
area_circle = area_circle + 1
return 4.0 * area_circle / area_square
pi = estimatePi()
print("pi = %10.9f"%(pi))
----------------------------------------------------------------------------------------
댓글 없음:
댓글 쓰기