본문 바로가기
파이썬

파이썬에서 한 줄씩 파일을 배열 요소로 읽기

by º기록 2021. 1. 19.
반응형

따라서 Ruby에서 다음을 수행 할 수 있습니다.

testsite_array = Array.new
y=0
File.open('topsites.txt').each do |line|
testsite_array[y] = line
y=y+1
end

파이썬에서 어떻게할까요?

 

해결 방법

 

testsite_array = []
with open('topsites.txt') as my_file:
    for line in my_file:
        testsite_array.append(line)

이는 Python을 사용하여 파일을 직접 반복 할 수 있기 때문에 가능합니다.


with open('topsites.txt') as my_file:
    testsite_array = my_file.readlines()

 

참조 페이지 https://stackoverflow.com/questions/16222956

 

 

반응형

댓글