Reputation: 105
I am trying to format text in a .txt file. The content is also in an xml, but I copied to a text file and I am trying to for it. It is currently set up like:
Pufferfish Ocean
Anchovy Ocean
Tuna Ocean
Sardine Ocean
Bream River
Largemouth_Bass Mountain_Lake
Smallmouth_Bass River
Rainbow_Trout River
I am trying to figure out how to open the file and for each line convert it to:
('Pufferfish', 'Ocean')
Is there a way to do this?
This is what I am trying so far, which I know is wrong, and I am trying to look up the correct syntax and way change 'str':
f1 = open('fish.txt', 'r')
f2 = open('fish.txt.tmp', 'w')
for line in f1:
f2.write(line.replace(' ', ','))
for word in line:
f2.write(word.append('(', [0]))
f2.write(word.append(')', (len(word))))
f1.close()
f2.close()
Upvotes: 2
Views: 3492
Reputation: 45231
Probably the most important tidbit you should learn from this exercise is: an str
object does not have any method like append()
or insert()
or the like. This is because str
objects- strings- are immutable objects in Python. You cannot CHANGE a string. You can only use it to make another new string (and throw away the old one).
Since your file format looks like the first space only appears at the locations where you want your comma inserted, you could use the replace()
method like you are trying to do, like so:
line = line.replace(' ', ', ', 1)
Note that the replace()
method on a string does not modify the original string; instead, it returns a new string. That is why you have to use the line =
part at the beginning of the line, thereby replacing the old string.
The third argument- the number 1- at the end makes sure that only the first space in the line is affected. If there are multiple spaces or any hanging spaces at the end, they will not be replaced.
Upvotes: 1
Reputation: 154
There are shorter ways of writing it, but here is one way to solve your problem of taking a simple text file and writing as you asked. Save your text file as something like ocean.txt
output = ""
with open("ocean.txt" ) as f:
for line in f:
line.strip()
line_fmt = ",".join( '"' + item + '"' for item in line.split())
output += ( "({})".format( line_fmt ) ) + "\n"
print(output)
# To save as a file:
with open('formatted.txt', 'w') as outfile:
outfile.write( output)
This opens a text file, and reads in each line. Then it strips off the newline characters. Then it splits the line apart, and adds " + item + ". Then it take this word in quotes, and joines all of them together with a comma
",".join(
Last, it adds this string to the overall output, and prints it out at the end.
Upvotes: 1
Reputation: 25181
A variation to Pedro Lobito's answer using str.format
for more precise control of the output string format:
with open('old.txt') as f_in, open("new.txt", "a") as f_out:
for line in f_in:
a, b = line.split()
f_out.write("('{}', '{}')\n".format(a, b))
Version with comma at the end of each line except the last line:
with open('old.txt') as f_in, open("new.txt", "a") as f_out:
for n, line in enumerate(f_in):
a, b = line.split()
if n > 0:
f_out.write(",\n")
f_out.write("('{}', '{}')".format(a, b))
# do not leave the last line without newline ("\n"):
f_out.write("\n")
enumerate
does this: list(enumerate(["a", "b", "c"]))
returns [(0, "a"), (1, "b"), (2, "c")]
Upvotes: 3
Reputation: 98901
you may need something like:
with open('input.txt') as input, open("output.txt", "a") as output:
for line in input:
output.write(str(tuple(line.split()))+"\n")
Output:
('Pufferfish', 'Ocean')
('Anchovy', 'Ocean')
('Tuna', 'Ocean')
('Sardine', 'Ocean')
('Bream', 'River')
('Largemouth_Bass', 'Mountain_Lake')
('Smallmouth_Bass', 'River')
('Rainbow_Trout', 'River')
Upvotes: 4