Read 2 files, add something in between each line - python

Here's my code:
eeee = input('\nWhat do you want to combine each other with? ')
first = []
second = []
with open('First.txt', 'r') as f:
for line in f.readlines():
first.append(line)
with open('Second.txt', 'r') as f:
for line in f.readlines():
second.append(line)
with open('NewStuff.txt', 'a') as f:
for thing in first:
for thing2 in second:
f.write(thing + str(eeee) + thing2)
I want to get first line from file1, add something in the middle of it (whatever eeee is inputted as) and then print the first line from file2 and then get second line and repeat

You could try something like this, using zip and openning multiple files:
eeee = input('\nWhat do you want to combine each other with? ')
with open('First.txt', 'r') as f1, open('Second.txt', 'r') as f2,open('NewStuff.txt', 'a') as fnew:
for first, second in zip(f1.readlines(),f2.readlines())
fnew.write(first.replace('\n','')+' '+ str(eeee)+' '+ second)

Since you want str(eeee) for all strings in second, you could just add it to the starting of all items of that list.
third = [str(eeee)+i for i in second]

Your code is almost correct, just change this part:
with open('NewStuff.txt', 'a') as f:
for thing in first:
for thing2 in second:
f.write(thing + str(eeee) + thing2)
to:
with open('NewStuff.txt', 'a') as f:
for thing,thing2 in zip(first,second):
f.write(thing + str(eeee) + thing2)

Since you're not combining mutiple lines of First.txt together, nor multiple lines of Second.txt with each other, I wouldn't use any solution involving readlines() as there's no reason to read either file completely into memory.
The most you need to read at any time is one line of each file. I'd suggest a solution along lines of:
eeee = input('\nWhat do you want to combine each other with? ')
with open('First.txt') as left, open('Second.txt') as right:
with open('NewStuff.txt', 'a') as output:
for line in left:
output.write(line.rstrip('\n') + eeee + right.readline())
And avoid any solution that preserves your calls to readlines() or introduces new ones. Note that we rstrip('\n') the end of the left line so that we end up with a single output line. Now you need to consider what happens if the two input files do not contain the same number of lines.

Related

Combine two wordlist in one file Python

I have two wordlists, as per the examples below:
wordlist1.txt
aa
bb
cc
wordlist2.txt
11
22
33
I want to take every line from wordlist2.txt and put it after each line in wordlist1.txt and combine them in wordlist3.txt like this:
aa
11
bb
22
cc
33
.
.
Can you please help me with how to do it? Thanks!
Try to always try to include what you have tried.
However, this is a great place to start.
def read_file_to_list(filename):
with open(filename) as file:
lines = file.readlines()
lines = [line.rstrip() for line in lines]
return lines
wordlist1= read_file_to_list("wordlist1.txt")
wordlist2= read_file_to_list("wordlist2.txt")
with open("wordlist3.txt",'w',encoding = 'utf-8') as f:
for x,y in zip(wordlist1,wordlist2):
f.write(x+"\n")
f.write(y+"\n")
Check the following question for more ideas and understanding: How to read a file line-by-line into a list?
Cheers
Open wordlist1.txt and wordlist2.txt for reading and wordlist3.txt for writing. Then it's as simple as:
with open('wordlist3.txt', 'w') as w3, open('wordlist1.txt') as w1, open('wordlist2.txt') as w2:
for l1, l2 in zip(map(str.rstrip, w1), map(str.rstrip, w2)):
print(f'{l1}\n{l2}', file=w3)
Instead of using .splitlines(), you can also iterate over the files directly. Here's the code:
wordlist1 = open("wordlist1.txt", "r")
wordlist2 = open("wordlist2.txt", "r")
wordlist3 = open("wordlist3.txt", "w")
for txt1,txt2 in zip(wordlist1, wordlist2):
if not txt1.endswith("\n"):
txt1+="\n"
wordlist3.write(txt1)
wordlist3.write(txt2)
wordlist1.close()
wordlist2.close()
wordlist3.close()
In the first block, we are opening the files. For the first two, we use "r", which stands for read, as we don't want to change anything to the files. We can omit this, as "r" is the default argument of the open function. For the second one, we use "w", which stands for write. If the file didn't exist yet, it will create a new file.
Next, we use the zip function in the for loop. It creates an iterator containing tuples from all iterables provided as arguments. In this loop, it will contain tuples containing each one line of wordlist1.txt and one of wordlist2.txt. These tuples are directly unpacked into the variables txt1 and txt2.
Next we use an if statement to check whether the line of wordlist1.txt ends with a newline. This might not be the case with the last line, so this needs to be checked. We don't check it with the second line, as it is no problem that the last line has no newline because it will also be at the end of the resulting file.
Next, we are writing the text to wordlist3.txt. This means that the text is appended to the end of the file. However, the text that was already in the file before the opening, is lost.
Finally, we close the files. This is very important to do, as otherwise some progress might not be saved and no other applications can use the file meanwhile.
Try this:
with open('wordlist1.txt', 'r') as f1:
f1_list = f1.read().splitlines()
with open('wordlist2.txt', 'r') as f2:
f2_list = f2.read().splitlines()
f3_list = [x for t in list(zip(f1, f2)) for x in t]
with open('wordlist3.txt', 'w') as f3:
f3.write("\n".join(f3_list))
with open('wordlist1.txt') as w1,\
open('wordlist2.txt') as w2,\
open('wordlist3.txt', 'w') as w3:
for wordlist1, wordlist2 in zip(w1.readlines(), w2.readlines()):
if wordlist1[-1] != '\n':
wordlist1 += '\n'
if wordlist2[-1] != '\n':
wordlist2 += '\n'
w3.write(wordlist1)
w3.write(wordlist2)
Here you go :)
with open('wordlist1.txt', 'r') as f:
file1 = f.readlines()
with open('wordlist2.txt', 'r') as f:
file2 = f.readlines()
with open('wordlist3.txt', 'w') as f:
for x in range(len(file1)):
if not file1[x].endswith('\n'):
file1[x] += '\n'
f.write(file1[x])
if not file2[x].endswith('\n'):
file2[x] += '\n'
f.write(file2[x])
Open wordlist 1 and 2 and make a line paring, separate each pair by a newline character then join all the pairs together and separated again by a newline.
# paths
wordlist1 = #
wordlist2 = #
wordlist3 = #
with open(wordlist1, 'r') as fd1, open(wordlist2, 'r') as fd2:
out = '\n'.join(f'{l1}\n{l2}' for l1, l2 in zip(fd1.read().split(), fd2.read().split()))
with open(wordlist3, 'w') as fd:
fd.write(out)

How to save each line of a file to a new file (every line a new file) and do that for multiple original files

I have 5 files from which i want to take each line (24 lines in total) and save it to a new file. I managed to find a code which will do that but they way it is, every time i have to manually change the number of the appropriate original file and of the file i want to save it to and also the number of each line every time.
The code:
x1= np.loadtxt("x_p2_40.txt")
x2= np.loadtxt("x_p4_40.txt")
x3= np.loadtxt("x_p6_40.txt")
x4= np.loadtxt("x_p8_40.txt")
x5= np.loadtxt("x_p1_40.txt")
with open("x_p1_40.txt", "r") as file:
content = file.read()
first_line = content.split('\n', 1)[0]
with open("1_p_40_x.txt", "a" ) as f :
f.write("\n")
with open("1_p_40_x.txt", "a" ) as fa :
fa.write(first_line)
print(first_line)
I am a beginner at python, and i'm not sure how to make a loop for this, because i assume i need a loop?
Thank you!
Since you have multiple files here, you could define their names in a list, and use a list comprehension to open file handles to them all:
input_files = ["x_p2_40.txt", "x_p4_40.txt", "x_p6_40.txt", "x_p8_40.txt", "x_p1_40.txt"]
file_handles = [open(f, "r") for f in input_files]
Since each of these file handles is an iterator that yields a single line every time you iterate over it, you could simply zip() all these file handles to iterate over them simultaneously. Also throw in an enumerate() to get the line numbers:
for line_num, files_lines in enumerate(zip(*file_handles), 1):
out_file = f"{line_num}_p_40.txt"
# Remove trailing whitespace on all lines, then add a newline
files_lines = [f.rstrip() + "\n" for f in files_lines]
with open(out_file, "w") as of:
of.writelines(files_lines)
With three files:
x_p2_40.txt:
2_1
2_2
2_3
2_4
x_p4_40.txt:
4_1
4_2
4_3
4_4
x_p6_40.txt:
6_1
6_2
6_3
6_4
I get the following output:
1_p_40.txt:
2_1
4_1
6_1
2_p_40.txt:
2_2
4_2
6_2
3_p_40.txt:
2_3
4_3
6_3
4_p_40.txt:
2_4
4_4
6_4
Finally, since we didn't use a context manager to open the original file handles, remember to close them after we're done:
for fh in file_handles:
fh.close()
If you have files with an unequal number of lines and you want to create files for all lines, consider using itertools.zip_longest() instead of zip()
In order to read each of your input files, you can store them in a list and iterate over it with a for loop. Then we add every line to a single list with the function extend() :
inputFiles = ["x_p2_40.txt", "x_p4_40.txt", "x_p6_40.txt", "x_p8_40.txt", "x_p1_40.txt"]
outputFile = "outputfile.txt"
lines = []
for filename in inputFiles:
with open(filename, 'r') as f:
lines.extend(f.readlines())
lines[-1] += '\n'
Finally you can write all the line to your output file :
with open(outputFile, 'w') as f:
f.write(''.join(lines))

Get result in one line loop

My output:
I have CMShehbaz
CMShehbaz
Expected:
I have CMShehbaz CMShehbaz
I am trying get result in one line. I tried with end="", concat +, but did not
work. I want result in one line.
lines = []
with open('user.txt') as f:
lines = f.readlines()
count = 0
for line in lines:
count += 1
print("I have {} {}".format(line,line) )
print(f'line {count}: {line}')
I'm not quite sure why you have a counter in there if all you want is a single string, but this will do that job.
user.txt
CMShehbaz1
CMShehbaz2
CMShehbaz3
python file
with open('user.txt') as f:
foo = "I have "
bar = " ".join(line.strip() for line in f)
print(foo+bar)
# Or you can do
foo = " ".join(line.strip() for line in f)
print(f"I have {foo}")
Gives you the output:
I have CMShehbaz1 CMShehbaz2 CMShehbaz3
If you want to know how many names are in foo then you can do
print(len(foo.split(' '))) # this will give you 3
I suspect that "user.txt" is a list of usernames each on a new line.
so in your code line will look something like "USER\n"
You can strip off the "\n" character of use some of the solutions posted previously: How to read a file without newlines?

Changing the contents of a text file and making a new file with same format

I have a big text file with a lot of parts. Every part has 4 lines and next part starts immediately after the last part.
The first line of each part starts with #, the 2nd line is a sequence of characters, the 3rd line is a + and the 4th line is again a sequence of characters.
Small example:
#M00872:462:000000000-D47VR:1:1101:15294:1338 1:N:0:ACATCG
TGCTCGGTGTATGTAAACTTCCGACTTCAACTGTATAGGGATCCAATTTTGACAAAATATTAACGCTTATCGATAAAATTTTGAATTTTGTAACTTGTTTTTGTAATTCTTTAGTTTGTATGTCTGTTGCTATTATGTCTACTATTCTTTCCCCTGCACTGTACCCCCCAATCCCCCCTTTTCTTTTAAAAGTTAACCGATACCGTCGAGATCCGTTCACTAATCGAACGGATCTGTCTCTGTCTCTCTC
+
BAABBADBBBFFGGGGGGGGGGGGGGGHHGHHGH55FB3A3GGH3ADG5FAAFEGHHFFEFHD5AEG1EF511F1?GFH3#BFADGD55F?#GFHFGGFCGG/GHGHHHHHHHDBG4E?FB?BGHHHHHHHHHHHHHHHHHFHHHHHHHHHGHGHGHHHHHFHHHHHGGGGHHHHGGGGHHHHHHHGHGHHHHHHFGHCFGGGHGGGGGGGGFGGEGBFGGGGGGGGGFGGGGFFB9/BFFFFFFFFFF/
I want to change the 2nd and the 4th line of each part and make a new file with similar structure (4 lines for each part). In fact I want to keep the 1st 65 characters (in lines 2 and 4) and remove the rest of characters. The expected output for the small example would look like this:
#M00872:462:000000000-D47VR:1:1101:15294:1338 1:N:0:ACATCG
TGCTCGGTGTATGTAAACTTCCGACTTCAACTGTATAGGGATCCAATTTTGACAAAATATTAACG
+
BAABBADBBBFFGGGGGGGGGGGGGGGHHGHHGH55FB3A3GGH3ADG5FAAFEGHHFFEFHD5A
I wrote the following code:
infile = open("file.fastq", "r")
new_line=[]
for line_number in len(infile.readlines()):
if line_number ==2 or line_number ==4:
new_line.append(infile[line_number])
with open('out_file.fastq', 'w') as f:
for item in new_line:
f.write("%s\n" % item)
but it does not return what I want. How to fix it to get the expected output?
This code will achieve what you want -
from itertools import islice
with open('bio.txt', 'r') as infile:
while True:
lines_gen = list(islice(infile, 4))
if not lines_gen:
break
a,b,c,d = lines_gen
b = b[0:65]+'\n'
d = d[0:65]+'\n'
with open('mod_bio.txt', 'a+') as f:
f.write(a+b+c+d)
How it works?
We first make a generator that gives 4 lines at a time as you mention.
Then we open the lines into individual lines a,b,c,d and perform string slicing. Eventually we join that string and write it to a new file.
I think some itertools.cycle could be nice here:
import itertools
with open("transformed.file.fastq", "w+") as output_file:
with open("file.fastq", "r") as input_file:
for i in itertools.cycle((1,2,3,4)):
line = input_file.readline().strip()
if not line:
break
if i in (2,4):
line = line[:65]
output_file.write("{}\n".format(line))
readlines() will return list of each line in your file. You don't need to prepare a list new_line. Directly iterate over index-value pair of list, then you can modify all the values in your desired position.
By modifying your code, try this
infile = open("file.fastq", "r")
new_lines = infile.readlines()
for i, t in enumerate(new_lines):
if i == 1 or i == 3:
new_lines[i] = new_lines[i][:65]
with open('out_file.fastq', 'w') as f:
for item in new_lines:
f.write("%s" % item)

python combine two text files to one file

i want to create a simple code to combine two text files , example file1.txt contain:
car
house
and file2.txt contain :
voiture
maison
i want to combine the lines of the two files and separte them by ':' to look like that :
car:voiture
house:maison
i try to do it and i'm sure that i'm wrong anyway i will post my code :) :
with open("user.txt") as u:
with open("site.txt") as s:
for line in s.read().split('\n'):
s1=line
for line in u.read().split('\n'):
s2=line
with open('result.txt', 'a') as file:
file.write(s1+':'+s2)
and thanks a lot for any help guys :)
This is a use case for itertools.izip:
from itertools import izip
with open('file1.txt') as f1, open('file2.txt') as f2, open('new.txt', 'w') as fout:
for fst, snd in izip(f1, f2):
fout.write('{0}:{1}\n'.format(fst.rstrip(), snd.rstrip()))
This combines the first line from the first file with the first line from the second file (then the second line from the first file with the second line from the second file etc...), removes newlines from the lines, adds a : in the middle and adds a \n so it's actually a line. This saves loading both files fully into memory and does it iteratively over each. Note though, that if the files are not of equal length, the result will stop at the number of lines in the shortest file.
Your code won't work because you try to read the whole second file for every line in the first one, also, there are a few other bugs (like not writing newlines, etc.). Try instead
with open("user.txt") as u, \
open("site.txt") as s, \
open("result.txt", "a") as file: # Only open every file once for all output
for s1 in u: # You don't nead to use .read().split('\n')
s2 = s.readline() # Read ONE line INCLUDING the newline char
file.write(s1 + ":" + s2) # Write output with the newline from `s2`
Or, you could just read all lines and use zip:
with open("user.txt") as u, \
open("site.txt") as s, \
open("result.txt", "a") as file:
user_lines = u.readlines()
size_lines = s.readlines()
for s1, s2 in zip(user_lines, size_lines):
file.write(s1 + ":" + s2 + "\n") # Write output with newline char
You’re close, but there’s a problem. Rather than iterating one after the other or one inside another, you need to iterate them both together. Fortunately, that’s easy with zip:
with open('user.txt', 'r') as user_file, \
open('site.txt', 'r') as site_file, \
open('result.txt', 'a') as result_file:
user_lines = user_file.read().split('\n')
site_lines = site_file.read().split('\n')
for user_line, site_line in zip(user_lines, site_lines):
result_file.write(user_line + ':' + site_line + '\n')
If you do that you'll read all of file u before file s get's to the second line.
You want to use file.readline() to iterate each file by one line at a time until you finish one or both files (and probably have something to deal with that last uneven line if the files aren't equal size.)
My solution:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
with open('result.txt', 'a') as r:
with open("user.txt") as u:
with open("site.txt") as s:
for line in u:
r.write(
"{0}: {1}".format(
line[:-1] if line.endswith('\n') else line,
s.readline()
)
)
Hope this helps!

Categories

Resources