2013-06-06 4 views
0

텍스트 파일이 있고 두 번째 줄에 1, 2, 3, 4, 5, 6, 7 및 8 번째 문자를 읽고 싶다면 어떻게해야합니까? 쓸까요? 나는 파이썬 아주 새로운 해요 및(), 난 그냥 문자 H, E, L, L를, (,)를 읽어 얼마나 V3.3에게 M을파이썬에서 특정 줄의 특정 문자를 읽는 방법

예 텍스트 파일

Hello, my name 
is Bob. How are 
you? 

을 사용하고 있습니다 , 및 Y?

+3

그 문자가 * 첫째 * 줄에서입니다 매우 큰 파일을 잘 작동하지 않습니다 .. –

답변

0

일반적으로 :

  1. 파일을 엽니 다
  2. 그것을 읽기 라인 라인
  3. 에 의해 당신이 읽고있는 라인이 2 일 경우, :
    1. 는 문자 줄의 문자를 읽어
    2. 문자가 첫 번째, 두 번째, 세 번째이면 .... 인쇄하십시오.

이의 파이썬 구체적인 정말 쉽습니다. 나는 당신이 무엇을 생각해 낼 수 있는지를 제안한다 - 당신은 그렇게 잘 배우게 될 것이다.

-1
# `f` is your file 
skip = 1 # in this case we want to skip one line to read characters from the second one 
for i in range(skip): # this loop will skip a number of lines 
    f.realine() 
line = f.readline() # read the line we were looking for 
chars = list(line[:8]) # getting first eight characters as a list 
2

그것은 어렵지 않다 : 당신은 파이썬 자습서를 읽고 시작해야

with open('filename.txt', 'r') as handle: 
    first_line = handle.readline() 

    print(first_line[0], first_line[1], ...) 

.

0

I는 다음과 같이 할 것 :

with open('thefile.txt') as thefile: 
    lines = thefile.readlines() # return a list with all the lines of the file 

# then to print 4th character of 6th line you do (mind zero-based indexing): 
print(lines[5][3]) 

그것은

관련 문제