기본적으로 저장된 파일의 행을 새로운 업데이트 된 번호로 업데이트하려고하지만 파일에 한 줄만 남겨 둡니다. 파일을 업데이트하는 대신 전체 파일을 덮어 쓰는 것처럼 느껴집니다. 나는 다른 질문을 여기에서 보았고, 그들이 나를 사용하기에 적합한 모듈을 주었지만 나는 가지고있는 문제를 파악할 수없는 것처럼 보였다.개체가 입력 파일로 반복 가능하지 않음
unique = 1
for line in fileinput.input('tweet log.txt', inplace=1):
if tweet_id in line: #checks if ID is unique, if it is not, needs to update it
tweet_fields = line.split(';')
old_count = tweet_fields[-2]
new_count = 'retweet=%d' % (int(tweet_retweet))
line = line.replace(old_count, new_count)
print line
unique = 0
if unique == 1: #if previous if didn't find uniqueness, appends the file
save_file = open('tweet log.txt', 'a')
save_file.write('id='+tweet_id +';'+
'timestamp='+tweet_timestamp+';'+
'source='+tweet_source+';'+
'retweet='+tweet_retweet+';'+'\n')
save_file.close()
매우 쉬운 해결책이 있지만 분명히 실종 상태입니다. 미리 감사드립니다.