2017-03-31 159 views
0

我创建了一个代码,要求用户在纯文本文件中输入位置列表,将用户在文本文件中输入的位置保存为列表,而不是要求用户在每个位置输入单词代表(与位置列表相同的顺序)结束重新创建句子。 但我不知道什么事情:打开并写入文件

1)我怎样才能使纯文本文件弹出只有当前一个关闭(所以只有当文件:list_of_numbers已关闭,其他文件list_of_words将弹出)。

2)如何将输出写入纯文本文件。

这里是代码:

import subprocess 
subprocess.Popen(["notepad","list_of_numbers.txt"]) 
with open("list_of_numbers.txt","r") as pos_file: 
    positions = pos_file.read().spllit() 

subprocess.Popen(["notepad","list_of_words.txt"]) 
with open("list_of_words.txt","r") as sentence_file: 
    words = sentence_file.read().split() 

mapping = dict(zip(positions, words)) 
output = [mapping[position] for position in positions] 

print(' '.join(output)) 

回答

0

林不知道你正在尝试做的完全是,但是这应该帮助。你并不需要使用子进程,而事实上它可能是一些过于复杂的事情......

输入:

Enter a number. (Leave blank to continue): 1 
Enter a number. (Leave blank to continue): 2 
Enter a number. (Leave blank to continue): 3 
Enter a number. (Leave blank to continue): 


Enter a word. (Leave blank to continue): a 
Enter a word. (Leave blank to continue): b 
Enter a word. (Leave blank to continue): c 
Enter a word. (Leave blank to continue): 

输出(控制台和OUTPUT.TXT):

1, a 

2, b 

3, c 

代码:

#!/usr/bin/env python 

while True: 

    positions = [] 
    words = [] 

    while True: 
     number = raw_input("Enter a number. (Leave blank to continue): ") 
     if not number: 
      break 

     try: 
      positions.append(int(number)) 
     except TypeError: 
      print("Invalid number provided. Try again.") 

    print("\n") 

    while True: 
     word = raw_input("Enter a word. (Leave blank to continue): ") 
     if not word: 
      break 

     words.append(word) 

    if len(positions) != len(words): 
     print("You did not enter the same number of words and positions..") 
     print("Clearing previous inputs..") 
    else: 
     break 

with open("output.txt", 'a') as output_file: 
    for x in xrange(0, len(positions)): 
     line = "{0}, {1}\n".format(positions[x], words[x]) 
     output_file.write(line) 
     print(line) 
+0

他使用Python3。 '尝试 - 除了'附加?自从什么时候追加提出任何异常? – DahliaSR

+0

@DahliaSR'try-except'不适用于'.append()',它适用于'int()' - 在用户输入非整数的情况下,它不能被转换为int。 (注意,它并不存在“while”字样下。 此外,更新了Python 3的打印语句。我错过了! – cmeadows

0

通过查阅Python文档了解如何实现这一点并不成问题。
- Popen
- Reading and Wirting Files


不失! - 如果你创建一个对象,那么你应该把它分配给一个变量!

# DO 
process = subprocess.Popen(["notepad","list_of_numbers.txt"]) 

# DONT 
subprocess.Popen(["notepad","list_of_numbers.txt"]) 

咨询Python文档应该带你到这样一个解决方案:

import subprocess 

# don't do anything unless child process has been terminated 
with subprocess.Popen(["notepad","list_of_numbers.txt"]) as process: 
    pass # you could leave this line blank but using pass clearer 

with open("list_of_numbers.txt","r") as pos_file: 
    positions = pos_file.read().split() 

process = subprocess.Popen(["notepad","list_of_words.txt"]) 

with open("list_of_words.txt","r") as sentence_file: 
    words = sentence_file.read().split() 

mapping = dict(zip(positions, words)) 
output = [mapping[position] for position in positions] 

# open a new file in write mode, write data and close the file 
with open('new_file.txt', 'w') as f: 
    f.write(' '.join(output)) 

注意:您仍然需要修复你的任务,以mappingoutput在17行和18,因为它们不会产生预期的结果,但这不是你问题的一部分。