1. ํ์ผ๋ณต์ฌ
ํ์ผ๋ณต์ฌ
์๋ณธ -> ๋ฒํผ ๋ณ์(Memory) -> ๋ณต์ฌ๋ณธ
buffer_size = 1024 #1024 byte -> 1 KB ์๋ฏธ
with open('../../Day07/Section13/hello.txt', 'rb') as source:
with open('hello2.txt', 'wb') as copy:
while True:
buffer = source.read(buffer_size)
if not buffer:
break
copy.write(buffer)
print('hello2.txt ํ์ผ์ด ๋ณต์ฌ ๋์์ต๋๋ค')
2. CSV ํ์ผ
CSV(comma-seperated values)
ํ๋๋ฅผ ์ผํ(,)๋ก ๊ตฌ๋ถํ
ํ
์คํธ ๋ฐ์ดํฐ ํ์ผ์ด๋ค.
์์ 1) csvReader
student_list = []
with open('ํ์๋ช
๋จ.csv', 'rt', encoding='UTF-8') as file:
file.readline()
while True:
line = file.readline()
if not line:
break
student = line.split(',')
student_list.append(student)
print(student_list)
์์ 2) csvWriter
#์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ
import csv
with open('์ฐจ๋๊ด๋ฆฌ.csv', 'w', encoding='UTF-8') as file:
csv_maker = csv.writer(file, delimiter=',')
csv_maker.writerow([1, '08๋ฌ1234', '2020-10-20,14:00'])
csv_maker.writerow([2, '25๋ค1234', '2020-10-20,14:10'])
csv_maker.writerow([3, '28ํ1234', '2020-10-20,14:20'])
print('์ฐจ๋๊ด๋ฆฌ.csv ํ์ผ์ด ์์ฑ๋์์ต๋๋ค.')
# ๋ ๋ฒ์งธ ๋ฐฉ๋ฒ
import csv
with open('์ฐจ๋๊ด๋ฆฌ.csv', 'w', newline='', encoding='UTF-8') as file:
csv_maker = csv.writer(file, delimiter=',') # ์ฝค๋ง๋ก ๊ตฌ๋ถํ๊ฒ ๋ค.
csv_maker.writerow([1, '08๋ฌ1234', '2020-10-20,14:00'])
csv_maker.writerow([2, '25๋ค1234', '2020-10-20,14:10'])
csv_maker.writerow([3, '28ํ1234', '2020-10-20,14:20'])
print('์ฐจ๋๊ด๋ฆฌ.csv ํ์ผ์ด ์์ฑ๋์์ต๋๋ค.')
# ๋ง์ง๋ง ๋ฐฉ๋ฒ
import csv
with open('์ฐจ๋๊ด๋ฆฌ.csv', 'w', newline='', encoding='UTF-8') as file:
csv_maker = csv.writer(file, delimiter=',', quotechar='"')
csv_maker.writerow([1, '08๋ฌ1234', '2020-10-20,14:00'])
csv_maker.writerow([2, '25๋ค1234', '2020-10-20,14:10'])
csv_maker.writerow([3, '28ํ1234', '2020-10-20,14:20'])
print('์ฐจ๋๊ด๋ฆฌ.csv ํ์ผ์ด ์์ฑ๋์์ต๋๋ค.')
์์ 3) csvQuoteReader
member_list = []
with open('ํ์๋ช
๋จ.csv', 'rt', encoding='UTF-8') as file:
file.readline() # ์ฒซ ์ค ์ ๊ฑฐ
while True:
line = file.readline()
if not line:
break
member = line.split(',')
member[0] = member[0].strip('"') # ์์ชฝ ์ ๊ฑฐ
member[2] = member[2].strip('\n')
member_list.append(member)
print(member_list)
์์ 4) csvReader
import csv
with open('์ฐจ๋๊ด๋ฆฌ.csv', 'r', newline='', encoding='UTF-8') as file:
csv_reader = csv.reader(file, delimiter=',', quotechar='"')
for line in csv_reader:
print(line)
'๐์น ๊ฐ๋ฐ(Web) > ๐ํ์ด์ฌ(Python)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ด์ฌ ์น ํฌ๋กค๋ง Beautiful Soup (2) | 2023.05.09 |
---|---|
ํ์ด์ฌ json (0) | 2023.02.08 |
ํ์ผ ๋ง๋ค๊ณ ์ฝ๊ธฐ (0) | 2023.02.06 |
์ง์ญ๋ณ์(local) ์ ์ญ๋ณ์(global) (0) | 2023.02.01 |
datetime ๋ชจ๋ (0) | 2023.02.01 |