2013-03-12 27 views
0

問題如何根據輸入添加輸出的中斷線?

我如何編寫代碼將在輸出插入插入斷裂線,那裏有一個輸入?

代碼

data1=[] 
with open("FileInput1.txt") as file: 
    for line in file: 
     data1.append([float(f) for f in line.strip().split()]) 


data2=[] 
with open("FileInput2.csv") as File2: 
    for line in File2: 
     data2.append([f for f in line.strip().split()]) 

樣品輸入:

  1. 文件輸入#1

    1223 32 (userID - int = 1223, work hours = 32) 
    2004 12.2 
    8955 80 
    

    一個。電流輸出

    1223 32 2004 12.2 8955 80 
    
  2. FileInput2:

    UserName 3423 23.6 
    Name  6743 45.9 
    

    一個。電流輸出

    UserName 3423 23.6 Name 6743 45.9 
    
+0

提供一個示例fileDoc.txt?在[pastebin]上(http://pastebin.com/),我會在接下來的5分鐘內嘲弄一些東西。 – Killrawr 2013-03-12 23:43:27

+0

請考慮使用內置的'csv'模塊。有很多相關的SO問題和答案。 – bernie 2013-03-12 23:44:50

+0

@Killrawr我剛剛添加了兩個不同的文件,我將從 – Masriyah 2013-03-12 23:49:27

回答

2

排除

基於神的(不要生氣)我已經破譯Amina's問題的神祕信息。這是導致這一切的談話:

So... this is a programming exercise in which the output is exactly 
the same as the input? 
– xxmbabanexx 12 mins ago 


@xxmbabanexx - lol yes – Amina 8 mins ago 

回答

爲了保持換行符在你的輸出,你需要簡單地打印確切文件了。如果我沒有上面的談話,我會給出一個更復雜的答案。這是一步一步給出的答案。

""" 
Task: Make a program which returns the exact same output as the input 
Ideas: 
print the output!! 
""" 

^這有助於我瞭解quesiton

first_input = "Input_One.txt" 
second_input = "Input_Two.txt" 



Input_One = open(first_input, "r") 
Input_Two = open (first_input, "r") 


Output_One = open("Output_One.txt", "w") 
Output_Two = open ("Output_Two.txt", "w") 

^我創建和打開我的兩個文件。

x = Input_One.read() 
y = Input_Two.read() 

^我讀的信息,分配給它的變量xy

print "OUTPUT 1:\n", x 
print "\n" 
print "OUTPUT 2:\n", y 

^I顯示輸出給用戶

#Save "output" 

Output_One.write(x) 
Output_Two.write(y) 

print"\nCOMPLETE!" 

^我保存輸出,並給一個消息。

+0

這應該適用於儘可能多的輸入文本文件。我沒有嘗試將它用於.csv文件。 – xxmbabanexx 2013-03-13 00:30:39

+0

你讓我發笑:D我會繼續嘗試,儘管我正在尋找一個簡單的解決方案,我可以在追加行結尾處有「\ n」嗎? – Masriyah 2013-03-13 00:51:43

+0

@Amina我需要做些什麼來獲得「神奇」的檢查。是的,我是一個*專家*(不是)極客幽默。 – xxmbabanexx 2013-03-13 01:28:09

1

你在做什麼只是合併的文件幾行成一條線。

def printer(filename, data): 
    with open(filename, "w") as f: 
    f.write(data.replace("\n", " ")) 


for filename in ["FileInput1.txt", "FileInput2.csv"]: 
    with open(filename) as f: 
    printer("new" + filename, f.read())