2013-03-20 2 views
0

내가 알아내는 데 문제가 있어요 왜 내가지고있어 ValueError를 '잘못된 문자베이스 (10)는 int()에 대한'addin.txt '잘못된 문자열 (10)

out = open('addout.txt', 'w') 

for line in open(int('addin.txt')): 
    line = line.strip() 
out.write((line [0] + line[1]) + (line [3] + line [4])) 
out.close 
+0

어, 왜 'addin.txt' 문자열을 int로 파싱합니까? 그것은 작동하지 않을 것입니다. – tjameson

+0

나는 파이썬에서 멍청한 사람이다. 같은 공백으로 구분 된 두 개의 정수를 읽는 프로그램을 만들려고 시도하고있다. –

+0

그리고 나서 합계를 addout.txt에 쓰려고 하는가? – tjameson

답변

1

여기의 수정 된 버전입니다 귀하의 코드, 당신이 유용하게 사용할 수 있기를 바랍니다.

# open the file in as writable 
out = open('addout.txt', 'w') 

# go through each line of the input text 
for line in open('addin.txt'): 
    # split into parts (will return two strings if there are two parts) 
    # this is syntax sugar for: 
    # parts = line.split() 
    # l = parts[0] 
    # r = parts[1] 
    l,r = line.split() 

    # parse each into a number and add them together 
    sum = int(l) + int(r) 

    # write out the result as a string, not forgetting the new line 
    out.write(str(sum)+"\n") 

# clean up your file handle 
out.close() 

코어 비트는 int(somestring)입니다. here을 읽으십시오. 다른베이스를 지정해야한다면 int(somestring, base=8)과 같습니다.

이 정보가 도움이되기를 바랍니다.

+0

고마워요! 정말 도움이되었습니다. –