# 파일 입출력
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='')
#
# list 저장
#
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()
#
# list 읽기; 읽어들인 결과는 문자
#
import csv
f=open('a.csv', 'r', encoding='utf-8')
reader=csv.reader(f)
for line in reader:
print(line)
f.close()
#
# 읽어들일 타입지정:
#
print()
import numpy as np
data = np.loadtxt('a.csv', delimiter=',', dtype=np.int32) #np.float32
print(data)
-----
참고
-----
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']
댓글 없음:
댓글 쓰기