728x90
csv ํ์ผ
๐ก๋ฐ์ดํฐ๊ฐ ์ฝค๋ง๋ก ๊ตฌ๋ถ๋ ํ
์คํธ ํ์ผ ํ์
csv ํ์ผ ์
์ถ๋ ฅ
csv ํ์ผ ์ฐ๊ธฐ
import csv
data=[
["์ด๋ฆ","๋ฐ","๋ฒํธ"],
["์ฌ์",1,20],
["ํ์ฒ ",3,8],
["ํ๋",5,32]
]
file=open("student.csv","w",newline="")
writer=csv.writer(file)
for d in data:
writer.writerow(d)
file.close()
open ์ newline=""์ ์ ์ผ๋ฉด ์๋์ค๋ฐ๊ฟ์ด ๋์ง์๋๋ค
csv ํ์ผ ์ฝ๊ธฐ
import csv
file=open("student.csv","r")
reader=csv.reader(file)
for data in reader:
print(data)
file.close()
import csv
def show_profit(data):
name=data[0] #์ข
๋ชฉ
purchase_price=int(data[1])#๋งค์
๊ฐ
amount=int(data[2])#์๋
target_price=int(data[3]) #๋ชฉํ๊ฐ
profit=(target_price-purchase_price)*amount #์์ต๊ธ
profit_ratio=(target_price/purchase_price-1)*100 #์์ต๋ฅ
print(f"{name} {profit} {profit_ratio:.2f}%")
#ํ์ผ ์ด๊ธฐ
file=open("./ํ์ผ์
์ถ๋ ฅ ๊ธฐ๋ณธ/mystock.csv","r",encoding='UTF-8') #๊ฒฝ๋ก ์ฃผ์ํ ๊ฒ
#์ธ์ฝ๋ฉ์ ๋ฃ์ด์ค์ผํจ
#ํ์ผ ๋ฐ์ดํฐ ์ฝ๊ธฐ
reader=list(csv.reader(file)) #reader๋ ๋ฆฌ์คํธ๊ฐ ์๋๊ธฐ๋๋ฌธ์ csv๋ฅผ ๋ฆฌ์คํธ๋ก ์๋ฃํ ๋ณํํด์ค์ผํจ
for data in reader[1:]:
show_profit(data)
file.close()
728x90
'{Programming} > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Python] ํ์ด์ฌ ์ฌ๋ผ์ด์ฑ ์ ๋ฆฌ (0) | 2025.04.18 |
|---|---|
| [Python] ํด๋์ค ์ ๋ฆฌ (0) | 2023.02.26 |
| [Python] ํ์ผ ์ ์ถ๋ ฅ ๊ธฐ๋ณธ (0) | 2023.01.22 |
| [Python] ๊ณผ์ 10์ฃผ์ฐจ (0) | 2023.01.08 |
| [Python] ๊ณผ์ 9์ฃผ์ฐจ (0) | 2023.01.08 |