그래서 파일을 읽고 각 단어를 문자열 목록에 저장하는 프로그램을 만들려고합니다. 각 줄을 문자열 목록에 추가 할 수 있지만 (아래 코드 참조) 각 개별 단어를 문자열 목록에 추가하는 방법은 무엇입니까? 이것은 미친 libs와 프로그램이기 때문에텍스트 파일의 각 단어를 목록에 저장하십시오.
또한, 나는 명사, 또는 신체 일부의 모양을 일부 문구를해야합니다. 기술적으로 두 개의 분리 된 단어이기 때문에 본문 부분을 한 문자열로 어떻게 목록에 저장합니까? 참조
코드 :
def main():
file_list = []
while True: #while loop that runs until the user puts in a file name...
#or types in quit
#asks the user for a file name, or gives them an option to quit
file_name = raw_input("Enter in the name of the file, or type in quit to quit: ")
if file_name == "quit":
sys.exit(0) #quits the program
else:
try: #attempts to open the file
fin = open(file_name)
break
except: #prints out if file name doesn't exist
print "No, no, file no here."
for eachLine in fin: #strips out the new lines from the file
file_list.append(eachLine.strip())
print file_list
if __name__ == '__main__':
main()
코드에 문제가 지금까지 무슨 일이야? – jamylak
텍스트 파일의 각 행을 목록에 추가하지만 각 개별 단어를 추가해야합니다. – user1768884