Reputation: 2505
Question
How can I write code which will insert insert break lines in the output, where there was one in the input?
Code
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()])
Sample Input:
File Input # 1
1223 32 (userID - int = 1223, work hours = 32)
2004 12.2
8955 80
a. Current Output
1223 32 2004 12.2 8955 80
FileInput2:
UserName 3423 23.6
Name 6743 45.9
a. Current Output
UserName 3423 23.6 Name 6743 45.9
Upvotes: 0
Views: 220
Reputation: 8696
Preclude
Based on the cryptic messages of the God's (please don't be offended) I have deciphered Amina's question. This was the conversation that led to it all:
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
Answer
To keep the newlines in your output, you need to simply print the exact file out. If I hadn't had the above conversation, I would have given a more complex answer. Here is the answer, given step-by-step.
"""
Task: Make a program which returns the exact same output as the input
Ideas:
print the output!!
"""
^This helps me to understand the 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")
^I create and open my two files.
x = Input_One.read()
y = Input_Two.read()
^I read the info, assigning it the the variables x
and y
print "OUTPUT 1:\n", x
print "\n"
print "OUTPUT 2:\n", y
^I show the output to the user
#Save "output"
Output_One.write(x)
Output_Two.write(y)
print"\nCOMPLETE!"
^I save the output and give a message.
Upvotes: 2
Reputation: 11543
What you are doing is just merging several lines in your file into a single line.
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())
Upvotes: 1