여기의 수정 된 버전입니다 귀하의 코드, 당신이 유용하게 사용할 수 있기를 바랍니다.
# 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)
과 같습니다.
이 정보가 도움이되기를 바랍니다.
어, 왜 'addin.txt' 문자열을 int로 파싱합니까? 그것은 작동하지 않을 것입니다. – tjameson
나는 파이썬에서 멍청한 사람이다. 같은 공백으로 구분 된 두 개의 정수를 읽는 프로그램을 만들려고 시도하고있다. –
그리고 나서 합계를 addout.txt에 쓰려고 하는가? – tjameson