2016-02-16 22 views
1

我有一個非常簡單的任務,即創建一個文本文件,其中包含從1-100開始的8個隨機整數,讀取文件,在同一行顯示數字,計算偶數和奇數整數,然後顯示它們。在一行顯示文件中的整數

我遇到的問題是讓字符串顯示在同一行上。我瀏覽過多篇關於類似問題的文章,但無濟於事。我試圖使用.join,但是,它包含它時似乎破壞了代碼。

# Imports random and time 
import random 
import time 

# Defines the main function 
def main(): 

    # Opens file "mynumbers" and creates it if not existent 
    myfile = open('mynumbers.txt', 'w') 

    # Statement to write intergers to text file in the correct format 
    for count in range(8): 
     number = random.randint(1, 100) 
     myfile.write(str(number) + '\n') 

# Defines read function 
def read(): 

    # Opens the "mynumbers" file created in the main function 
    myfile= open('mynumbers.txt', 'r') 

    # Sets the content variable to the content of the file that was opened 
    content = myfile.read() 

    # Prints the content variable and strips the \n from the string 
    stripit = content.rstrip('\n') 
    print(stripit) 


# Calls for the functions, prints created, and sleep calls 
main() 
print('File Created!') 
time.sleep(1) 
read() 
time.sleep(5) 

任何可以提供的幫助將不勝感激。

回答

1

read函數讀取整個文件內容爲一個字符串。您對該字符串的調用rstrip將從中刪除最後一個換行符,但不會刪除任何內部換行符。你不能有效地使用str.join,因爲你只有一個字符串。

我認爲有兩個合理的解決方案。首先是留在只是一個單一的字符串,但用空格代替所有的內部換行符:

def read(): 
    myfile = open('mynumbers.txt', 'r') 
    content = myfile.read() 
    stripit = content.rstrip('\n') 
    nonewlines = stripit.replace('\n', ' ') 
    print(nonewlines) 

另一種方法是到了分裂的單串入不同的字符串列表,每個號碼。如果我們稍後需要與他們做不同的事情,這會更有用。當然,我們所要做的就是使用join將它們合併到一起:

def read(): 
    myfile = open('mynumbers.txt', 'r') 
    content = myfile.read() 
    content_list = content.split() # by default, splits on any kind of whitespace 
    rejoined_content = " ".join(content_list) 
    print(rejoined_content) 
+0

真的很感激!第一個正是我所期待的。感謝大家所有的快速反應! –

0

該代碼看起來不錯,但在read()函數中執行此操作。

def read(): 
    my_numbers = [] 
    with open('mynumbers.txt', 'r') as infile: 
      for line in infile: 
       line = line.strip() 
       my_numbers.append(line) 
    print (' '.join(line)) 
1

寫入文件時不要添加換行符。只需使用代替空格(或逗號,等等)

import random 
import time 

#Defines the main function 
def main(): 

#Opens file "mynumbers" and creates it if not existent 
    myfile = open('mynumbers.txt', 'w') 

#Statement to write intergers to text file in the correct format 
    for count in range(8): 
     number = random.randint(1,100) 
     myfile.write(str(number) +' ') 

#Defines read function 
def read(): 

#Opens the "mynumbers" file created in the main function 
    myfile= open('mynumbers.txt', 'r') 

#Sets the content variable to the content of the file that was opened 
    content=myfile.read() 

#Prints the content variable and strips the \n from the string 
    print(content) 



#Calls for the functions, prints created, and sleep calls 
main() 
print('File Created!') 
time.sleep(1) 
read() 
time.sleep(5) 
+1

儘管我同意這是一個解決方案,但該程序的重​​點在於將\ n轉換爲單行,因此它不適用於此場景。 –

0

我會做這樣的,尤其是因爲你提到,你下一步需要做的偶數和奇數部分。在第一次循環結束時,您將得到一個可以使用的int列表(而不是strs),並確定它們是偶數還是奇數。

def read(): 
    my_nums = [] 
    with open('mynumbers.txt', 'r') as f: 
      for line in f: 
       num_on_line = int(line.strip()) 
       my_nums += [num_on_line] 
       print num_on_line, #don't forget that comma 
    for num in my_nums: 
      #display the even and odds 
0

你可以打印這樣

with open('mynumbers.txt', 'r') as numbers_file: 
    for line in numbers_file: 
     print(line.strip(), end=" ") 

line.strip()是消除\n字符在一行中的數字。