๐Ÿ“—
JunegLee's TIL
  • TIL
  • python
    • class
    • String Basic
    • regularExpression
    • String function
    • Generator
    • String format
    • getset
    • module
    • while
    • numpy
    • print()
    • matplotlib
    • for
    • Boolean
    • tuple
    • package
    • input(variable)
    • list
    • if
    • file
    • type()
    • pandas
    • function
    • dictionary
    • ๊ตฌ๋ฌธ ์˜ค๋ฅ˜์™€ ์˜ˆ์™ธ
    • builtinFunction
    • Constructor
  • algorithm
    • sort
      • mergeSort
      • insertionSort
      • bubbleSort
      • heapSort
      • quickSort
      • selectionSort
    • recursion
    • Greedy
    • DepthFirstSearch
    • basic
      • DataStructure
    • hash
    • BreadthFirstSearch
  • tensorflow
    • keras
      • layers
        • Flatten
        • Flatten
        • Dense
        • Dense
        • Conv2D
        • Conv2D
    • tensorflow1x
    • tensorflow2x
  • DB
    • setting
    • join
    • subQuery
    • overview
  • deep-learning
    • neuralNetwork
    • perceptron
    • neuralNetworkLearning
    • convolution neural network
    • Gradient Descent
    • Linear Regression
    • backPropagation
    • logistic regression
    • overview
  • textPreprocessing
    • overview
  • java
    • basics
      • generic
      • Variable
      • String
    • theory
      • Object Oriented Programing
  • NLP
    • Embedding
    • Natural Language Processing
Powered by GitBook
On this page
  • ํŒŒ์ผ ์—ด๊ณ  ๋‹ซ๊ธฐ : open(), close()
  • with ํ‚ค์›Œ๋“œ
  • ํ…์ŠคํŠธ ์ฝ๊ธฐ : read()
  • ํ…์ŠคํŠธ ์ฝ๊ธฐ : readline()
  • ํ…์ŠคํŠธ ์ฝ๊ธฐ :readlines()
  • ํ…์ŠคํŠธ ํ•œ ์ค„์”ฉ ์ฝ๊ธฐ : CSV

Was this helpful?

  1. python

file

ํŒŒ์ผ ์—ด๊ณ  ๋‹ซ๊ธฐ : open(), close()

  • ํŒŒ์ผ ์ฒ˜๋ฆฌ

    ํŒŒ์ผ์€ ํฌ๊ฒŒ ํ…์ŠคํŠธ ํŒŒ์ผ๊ณผ ๋ฐ”์ด๋„ˆ๋ฆฌ ํŒŒ์ผ๋กœ ๋‚˜๋‰จ

    ํŒŒ์ผ์„ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด ์ผ๋‹จ ํŒŒ์ผ์—ด๊ธฐ๋ฅผ ํ•˜๊ณ , ํŒŒ์ผ์„ ์—ด๋ฉด ํŒŒ์ผ ์ฝ๊ธฐ ๋˜๋Š” ํŒŒ์ผ ์“ฐ๊ธฐ๋ฅผ ํ•œ๋‹ค

  • open() : ํŒŒ์ผ ๊ฐ์ฒด = open(๋ฌธ์ž์—ด: ํŒŒ์ผ๊ฒฝ๋กœ, ๋ฌธ์ž์—ด: ๋ชจ๋“œ)

  • close() : ํŒŒ์ผ๊ฐ์ฒด.close()

    ํŒŒ์ผ์—ด๊ธฐ๋ชจ๋“œ

    ์„ค๋ช…

    w

    write๋ชจ๋“œ (์ƒˆ๋กœ์“ฐ๊ธฐ ๋ชจ๋“œ)

    a

    append ๋ชจ๋“œ (๋’ค์— ์ด์–ด์„œ ์“ฐ๊ธฐ ๋ชจ๋“œ)

    r

    read ๋ชจ๋“œ (์ฝ๊ธฐ ๋ชจ๋“œ)

file = open("basic.txt", "w") # w : write๋ชจ๋“œ (์ƒˆ๋กœ์“ฐ๊ธฐ ๋ชจ๋“œ)
file.write("Hello Python Programming...!")
file.close()

with ํ‚ค์›Œ๋“œ

  • open() ํ•จ์ˆ˜์™€ close() ํ•จ์ˆ˜ ์‚ฌ์ด์— ๋งŽ์œผ ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐ€, ์—ด๊ณ  ๋‹ซ์ง€ ์•Š๋Š” ์‹ค์ˆ˜ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๋Š” ๊ธฐ๋Šฅ

    with open(๋ฌธ์ž์—ด: ํŒŒ์ผ๊ฒฝ๋กœ, ๋ฌธ์ž์—ด: ๋ชจ๋“œ) as ํŒŒ์ผ ๊ฐ์ฒด:
        ๋ฌธ์žฅ
    with open("basic.txt", "w") as file:
      file.write("Hello Python Programming...!")

ํ…์ŠคํŠธ ์ฝ๊ธฐ : read()

ํŒŒ์ผ ๊ฐ์ฒด.read()
with open("basic.txt", "r") as file:
    contents = file.read()
print(contents)

ํ…์ŠคํŠธ ์ฝ๊ธฐ : readline()

  • readline()์„ ์‚ฌ์šฉํ•ด์„œ ํŒŒ์ผ์˜ ์ฒซ ๋ฒˆ์งธ ์ค„์„ ์ฝ์–ด ์ถœ๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ

    f = open("C:/user/์ƒˆํŒŒ์ผ.txt", 'r')
    line = f.readline()
    print(line)
    f.close()

ํ…์ŠคํŠธ ์ฝ๊ธฐ :readlines()

  • readlines ํ•จ์ˆ˜๋Š” ํŒŒ์ผ์˜ ๋ชจ๋“  ์ค„์„ ์ฝ์–ด์„œ ๊ฐ๊ฐ์˜ ์ค„์„ ์š”์†Œ๋กœ ๊ฐ–๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ๋Œ๋ ค์ค€๋‹ค.

    f = open("C:/user/์ƒˆํŒŒ์ผ.txt", 'r')
    lines = f.readlines()
    for line in lines:
      print(line)
    f.close()

ํ…์ŠคํŠธ ํ•œ ์ค„์”ฉ ์ฝ๊ธฐ : CSV

  • ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์กฐ์ ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ CSV, XML, JSON ๋“ฑ์ด ์žˆ๋‹ค

  • CSV ํŒŒ์ผ์€ ํ•œ์ค„์— ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ๊ฐ๊ฐ์˜ ์ค„์€ ์‰ผํ‘œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ๋ถ„

    ```python

    import random

    hanguls = list("๊ฐ€๋‚˜๋‹ค๋ผ๋งˆ๋ฐ”์‚ฌ์•„์ž์ฐจ์นดํƒ€ํŒŒํ•˜")

with open("info.txt", "w") as file: for i in range(1000): name = random.choice(hanguls) + random.choice(hanguls) weight = random.randrange(40, 100) height = random.randrange(140, 200) file.write("{}, {}, {}\n".format(name, weight, height))

### ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ์ค„ ์”ฉ ์ฝ์–ด ๋“ค์ผ ๋•Œ๋Š” for ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉ

for ํ•œ์ค„์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด in ํŒŒ์ผ ๊ฐ์ฒด ์ฒ˜๋ฆฌ

```python 
with open("info.txt", "r") as file:
    for line in file:
        (name, weight, height) = line.strip().split(", ")

        # ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฌธ์ œ์—†๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค: ๋ฌธ์ œ๊ฐ€ ์žˆ์œผ๋ฉด ์ง€๋‚˜๊ฐ
        if (not name) or (not weight) or (not height):
            continue

        # ๊ฒฐ๊ณผ๋ฅผ ๊ณ„์‚ฐ
        bmi = int(weight) / ((int(height) / 100) **2)
        result = ""
        if 25 <= bmi:
            result = "๊ณผ์ฒด์ค‘"
        elif 18.5 <= bmi:
            result = "์ •์ƒ ์ฒด์ค‘"
        else:
            result = "์ €์ฒด์ค‘"

        print('\n'.join([
            "์ด๋ฆ„: {}",
            "๋ชธ๋ฌด๊ฒŒ: {}",
            "ํ‚ค: {}",
            "BMI: {}",
            "๊ฒฐ๊ณผ: {}"
        ]).format(name, weight, height, bmi, result))
        print()
PreviousifNexttype()

Last updated 4 years ago

Was this helpful?