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. 文件输入 2:

    UserName  3423 23.6  
    Name      6743 45.9
    

    一个。电流输出

    UserName 3423 23.6 Name 6743 45.9
    
4

2 回答 2

2

排除

根据上帝的神秘信息(不要被冒犯),我已经破译了阿米娜的问题。这是导致这一切的对话:

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!!
"""

^这有助于我理解问题

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

^我向用户显示输出

#Save "output"

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

print"\nCOMPLETE!"

^我保存输出并给出消息。

于 2013-03-13T00:28:44.310 回答
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())
于 2013-03-13T00:26:33.163 回答