I need to read from 3 txt files and merge them into one big txt file.
Ex text file1:
John
Mary
Joe
Ex text file2:
Alabama
Alaska
Michigan
Ex text file3:
Maybe
Attending
Not Attending
I'm not sure what else to add to my code
path = '/home/pi/Documents/Test/name.txt'
file1 = open (path, 'r')
name = file1.read()
statepath = '/home/pi/Documents/Test/state.txt'
file2 = open (path, 'r')
states = file2.read()
statuspath = '/home/pi/Documents/Test/status.txt'
file3 = open(statuspath, 'r')
status = file3.read()
finalpath = '/home/pi/Documents/Test/final.txt'
file4 = open(finalpath, 'w')
final = file4.read()
for item in name, states, status:
final.write(file1, "\n")
final.write(file2, "\n")
final.write(file3, "\n")
file1.close()
file2.close()
file3.close()
final.close()
final expected output of the file is
John <------- first value in file1
Alabama <------ first value in file2
Maybe <------- first value in file 3
Mary <---------- second value in file 1
Alaska
Attending
Joe
Michigan
Not Attending
Basically trying to loop through all of them and print them sequentially
not sure how to loop.
First of all you are writing in final without actually ever reading anything so it can't work. Replace file1, file2, file3 with the variables that have the read() attribute.
Just use a for statement with each variable you want to loop. Like this:
for i in name:
for j in states:
for k in status:
all = i + '\n` + j + '\n' + k + '\n'
final.write(all)
One of possible solution, but you should be sure that you have the same length of 3 files.
def main():
name_path = 'name.txt'
state_path = 'state.txt'
status_path = 'status.txt'
final_path = 'final.txt'
with open(name_path, 'r') as file1, open(state_path, 'r') as file2, open(status_path, 'r') as file3, open(final_path, 'w') as final:
for line in file1.readlines():
final.write(line)
final.write(file2.readline())
final.write(file3.readline())
Some way of doing this for a general case, using itertools:
import itertools as it
files = [
'/home/pi/Documents/Test/name.txt',
'/home/pi/Documents/Test/state.txt',
'/home/pi/Documents/Test/status.txt'
]
def loadData(fpath):
with open(fpath, "r") as f:
yield from f.redlines()
with open('/home/pi/Documents/Test/final.txt') as f:
for e in it.chain.from_iterable(zip(*map(loadDAta, files))):
f.write(e)
I just slightly improved Netwave version and it seems to be the right pythonic way to solver this task, the full code will be something like this
import itertools as it
def load_data(fpath):
with open(fpath, 'r') as f:
for line in f.readlines():
yield line
def main():
files = [
'/home/pi/Documents/Test/name.txt',
'/home/pi/Documents/Test/state.txt',
'/home/pi/Documents/Test/status.txt'
]
with open('/home/pi/Documents/Test/final.txt', 'w') as f:
for e in it.chain.from_iterable(zip(*map(load_data, files))):
for line in e:
f.write(line)
if __name__ == '__main__':
main()
Related
I would like to open a text file named file1.txt, count the length, then close it with the name file2.txt.
I very much prefer not importing anything.
file1 = open('file1.txt')
def wordCount(file1):
contents = file1.read()
file1.close()
print(contents)
return len(contents)
print(wordCount(file1))
It comes out as it should but I have no idea where begin the next step of the process.
# Copying then editing files
local_saved = []
with open("file1.txt", "r") as f:
local_saved.append(f.read())
with open("file2.txt", "w+") as f:
text_to_write = local_saved[0]
# Edit text_to_write below.
# Can be deleted (text_to_write = "") or changed in any way as if it were a normal string.
# ...
f.write(text_to_write)
Here it is:
file1 = open('file1.txt')
def wordCount(file1):
contents = file1.read()
file2=open('file2.txt','w')
file2.write(contents)
file2.close()
file1.close()
print(contents)
return len(contents)
print(wordCount(file1))
I'm making program that open txt file and replace first 0 with 1 of given line. Now it only print the edited line, but I want that it prints all the lines. I'm using python 3.1.
line_number = 3
with open(filename, "r") as f:
number = 0
for line in f:
number += 1
if line_number == number:
content = line.replace("0","1",1)
savefile = filename[:4] + ".tmp"
with open(savefile, "w") as f:
f.write(content)
os.remove(filename)
os.rename(savefile, filename)
Text file:
0 Dog
0 Cat
0 Giraffe
0 Leopard
0 Bear
You need to write each unchanged line to the savefile:
import os
filename = 'input.txt'
line_number = 3
savefile = filename[:4] + ".tmp"
with open(filename, "r") as f:
with open(savefile, "w") as fout:
number = 0
for line in f:
number += 1
if line_number == number:
content = line.replace("0","1",1)
fout.write(content)
else:
# Write unchanged lines here
fout.write(line)
os.remove(filename)
os.rename(savefile, filename)
Did you try something like this:
filename = "./test.txt"
with open(filename) as f:
lines = f.readlines()
# the element with index 2 is the 3-th element
lines[2] = lines[2].replace("0","1",1)
with open(filename, 'w') as f:
[f.write(line) for line in lines]
Output(./test.txt):
0 Dog
0 Cat
1 Giraffe
0 Leopard
0 Bear
You can read the file and save it to a list. Then you can then perform a certain action for each item(or for a specific element) in the list and save the result in the same file. You don't need of .tmp file or to remove and rename a file.
Edit:
There is an another approach with fileinput (thanks to #PeterWood)
import fileinput
with fileinput.input(files=('test.txt',), inplace=True) as f:
for line in f:
if fileinput.lineno() is 3:
print(line.replace("0", "1", 1).strip())
else:
print(line.strip())
My actual code :
import os, os.path
DIR_DAT = "dat"
DIR_OUTPUT = "output"
filenames = []
#in case if output folder doesn't exist
if not os.path.exists(DIR_OUTPUT):
os.makedirs(DIR_OUTPUT)
#isolating empty values from differents contracts
for roots, dir, files in os.walk(DIR_DAT):
for filename in files:
filenames.append("output/" + os.path.splitext(filename)[0] + ".txt")
filename_input = DIR_DAT + "/" + filename
filename_output = DIR_OUTPUT + "/" + os.path.splitext(filename)[0] + ".txt"
with open(filename_input) as infile, open(filename_output, "w") as outfile:
for line in infile:
if not line.strip().split("=")[-1]:
outfile.write(line)
#creating a single file from all contracts, nb the values are those that are actually empty
with open(DIR_OUTPUT + "/all_agreements.txt", "w") as outfile:
for fname in filenames:
with open(fname) as infile:
for line in infile:
outfile.write(line)
#finale file with commons empty data
#creating a single file
with open(DIR_OUTPUT + "/all_agreements.txt") as infile, open(DIR_OUTPUT + "/results.txt", "w") as outfile:
seen = set()
for line in infile:
line_lower = line.lower()
if line_lower in seen:
outfile.write(line)
else:
seen.add(line_lower)
print("Psst go check in the ouptut folder ;)")
The last lines of my code are checking wether or not, element exists mutliple times. So, may the element exists, once, twice, three, four times. It will add it to results.txt.
But the thing is that I want to save it into results.txt only if it exists 4 times in results.txt.
Or best scenario, compare the 4 .txt files and save elements in commons into results.txt.
But I can't solve it..
Thanks for the help :)
To make it easier,
with open(DIR_OUTPUT + "/all_agreements.txt") as infile, open(DIR_OUTPUT + "/results.txt", "w") as outfile:
seen = set()
for line in infile:
if line in seen:
outfile.write(line)
else:
seen.add(line)
Where can I use the .count() function ?
Because I want to do something like xxx.count(line) == 4 then save it into resulsts.txt
If your files are not super big you can use set.intersection(a,b,c,d).
data = []
for fname in filenames:
current = set()
with open(fname) as infile:
for line in infile:
current.add(line)
data.append(current)
results = set.intersection(*data)
You also don't need to create one single big file for this issue.
Not sure how your input looks like or what output is expected...
But maybe this can spark some ideas:
from io import StringIO
from collections import Counter
lines = ["""\
a=This
b=is
c=a Test
""", """\
a=This
b=is
c=a Demonstration
""", """\
a=This
b=is
c=another
d=example
""", """\
a=This
b=is
c=so much
d=fun
"""]
files = (StringIO(l) for l in lines)
C = Counter(line for f in files for line in f)
print([k for k,v in C.items() if v >= 4])
# Output: ['a=This\n', 'b=is\n']
I need to read the names from the babynames2014.txt file and then create two new files, separating the boys and girls names. The resulting files should be called boynames2014.txt and girlnames.txt. The babynames2014.txt files looks like this:
1 Noah Emma
2 Liam Olivia
3 Mason Sophia
4 Jacob Isabella
and continues until it reaches 100 boy and girls names.
The code I have written so far creates both of the new text files but the boynames2014 contains nothing and the girlnames2014 contains only the name Noah with the number 1 before it like this: 1Noah.
I think that I will need to use readline() and line.split()
somewhere, I'm just not sure where and how to use them correctly. I also need to use a try/except block to handle the exception in case the babynames2014.txt file is not found.
infile = open("babynames2014.txt", "r")
outfile = open("boynames2014.txt", "w")
outfile = open("girlnames2014.txt", "w")
line = infile.readline()
datafield = line.split()
boyname2014 = datafield[0]
girlname2014 = datafield[1]
outfile.write(boyname2014)
outfile.write(girlname2014)
infile.close()
outfile.close()
I have only studied Python for 2-3 months and really appreciate any advice to help me learn more!
I've noticed one thing that is logically not correct i.e., outfile for both boynames2014.txt and girlnames2014.txt
You should've done like this.
infile = open("babynames2014.txt", "r")
outfile_boys = open("boynames2014.txt", "w")
outfile_girls = open("girlnames2014.txt", "w")
Then, you have to read the infile and split by new line for required data as following.
lines = infile.read().split("\n")
Then iterate over the lines as below and split by space(default).
for line in lines:
datafield = line.split()
boyname2014 = datafield[1]
girlname2014 = datafield[2]
outfile_boys.write(boyname2014 + '\n')
outfile_girls.write(girlname2014 + '\n')
I've selected 1 and 2 index for data field because your file contains data like :
1 boy_name girl_name
Splitting by space delivers boy_name to 1st index and girl_name to 2nd index
Then close your files as usual.
infile.close()
outfile_boys.close()
outfile_girls.close()
Hope it helps!
You need to have seperate pointers for output files.
`
infile = open("babynames2014.txt", "r")
outfileboy = open("boynames2014.txt", "w")
outfilegirl = open("girlnames2014.txt", "w")
for line in infile.readlines():
names = line.split(" ")
outfileboy.write(str(names[1]+"\n")
outfilegirl.write(str(names[2]+"\n")
outfileboy.close()
outfilegirl.close()
`
outfile1 = open("boynames2014.txt", "w")
outfile2 = open("girlnames2014.txt", "w")
with open('babynames2014.txt') as infile:
for line in infile:
datafield = line.split()
boyname2014 = datafield[0]
girlname2014 = datafield[1]
outfile1.write(boyname2014)
outfile2.write(girlname2014)
outfile1.close()
outfile2.close()
readline() only reads a single line (as the name might suggest)
so only the first line get read (1 Noah Emma )
To read the all the lines and split them and write them to a file try:
# use two different names for the files
# you had one name `outfile` which was being
# overwritten so tht why boy file was empty
infile = open("babynames2014.txt", "r")
boyfile = open("boynames2014.txt", "w")
girlfile = open("girlnames2014.txt", "w")
with open('babynames2014', 'r') as f:
for l in f.readlines(): # notice readlines instead of readline
_, boy, girl = l.split() # assumes the separator is a space
print(boy, file=boyfile)
print(girl, file=girlfile)
# don't forget to close your file desciptors
boyfile.close()
girlfile.close()
Here you go,
#! /usr/bin/python
import sys
boy_file = str(sys.argv[1])
girl_file = str(sys.argv[2])
all_records = [line.strip() for line in open('babynames2014', 'r')]
f1 = open(boy_file, "w")
f2 = open(girl_file, "w")
for record in all_records:
split_record = record.split(' ')
boy_name = split_record[1]
girl_name = split_record[2]
f1.write(boy_name+"\n")
f2.write(girl_name+"\n")
f1.close()
f2.close()
You have specified the same variable name both the output files. outfile.
infile = open("babynames2014.txt", "r")
outfileb = open("boynames2014.txt", "w")
outfileg = open("girlnames2014.txt", "w")
line = infile.readline()
datafield = line.split()
boyname2014 = datafield[0]
girlname2014 = datafield[1]
outfileb.write(boyname2014)
outfileg.write(girlname2014)
infile.close()
outfileb.close()
outfileg.close()
and you need to loop through the input file in order to get all the names.
You can use ''.join([i for i in s if not i.isdigit()]) to remove the number from the names.
infile = open("babynames2014.txt", "r")
outfileb = open("boynames2014.txt", "w")
outfileg = open("girlnames2014.txt", "w")
tmp = infile.readline()
line=''.join([i for i in tmp if not i.isdigit()])
datafield = line.split()
boyname2014 = datafield[0]
girlname2014 = datafield[1]
outfileb.write(boyname2014)
outfileg.write(girlname2014)
infile.close()
outfileb.close()
outfileg.close()
Want to consider a regex solution?
with open("babynames2014.txt", "r") as f1,open("boynames2014.txt", "w") as boys,open("girlnames2014.txt","w") as girls:
# Note this will not work for name which has speacial charecters like `-,$,etc`
boy_regex = re.compile(r"^\d\s?([a-zA-z0-9]+)\s[a-zA-z0-9]+$",re.MULTILINE)
girl_regex = re.compile(r"^\d\s?[a-zA-z0-9]+\s([a-zA-z0-9]+)$",re.MULTILINE)
boys.write('\n'.join(boy_regex.findall(f1.read())))
girls.write('\n'.join(girl_regex.findall(f1.read())))
I have txt with a number of lines (x#y). Each file has two parts (x, y) separated by a particular symbol (#). How would a python script that reads each line in a txt and adds a new line under each existing line, where the order of the two parts (x#y) is inverted (y#x).
What I'm trying to do presented as input/output:
INPUT:
x1#y1
x2#y2
x3#y3
OUTPUT:
x1#y1
y1#x1
x2#y2
y2#x2
x3#y3
y3#x3
How can this be done with python?
Here's one way:
infilename = 'in.dat'
outfilename = 'out.dat'
sep = '#'
with open(infilename) as infile, open(outfilename,'w') as outfile:
for line in infile:
split = line.strip().partition(sep)
outfile.write(line)
outfile.write(''.join(reversed(split)) + '\n')
and then
~/coding$ cat in.dat
x1#y1
x2#y2
x3#y3
~/coding$ python inverter.py
~/coding$ cat out.dat
x1#y1
y1#x1
x2#y2
y2#x2
x3#y3
y3#x3
Assumes the name of your file is bar.txt, and that you want to write it back to bar.txt. It also does no error checking nor cares about memory usage.
if __name__ == "__main__":
myfile = open("bar.txt", "rb")
lines = myfile.readlines()
myfile.close()
myfile = open("bar.txt", "wb")
for l in lines:
ls = l.strip()
myfile.write(ls + "\n")
lsplit = ls.split("#")
myfile.write(lsplit[1] + "#" + lsplit[0] + "\n")
myfile.close()
There are cleaner ways to do this, but you could use something like:
f = open('my_file.txt', 'r')
lines = f.readlines()
f.close()
outfile = open('my_file2.txt', 'w')
# write each line, followed by flipped line
for line in lines:
outfile.write('%s\n' % line)
parts = line.split('#')
outfile.write('%s#%s\n' % [parts[1], parts[0]])
outfile.close()
You can use open and read function to read your file and than use this function,
>>> st = "x1#y1"
>>> def myfunc(string):
... mylist = re.split(r'(#)',string)
... mylist.reverse()
... print "".join(mylist), string
...
>>> myfunc(st)
y1#x1 x1#y1
and than use write to write the strings into your new file.
def swap(delimiter="#", input="input.txt", ouput="output.txt"):
with open(input, "r") as input_file, open(ouput, "w") as output_file:
for line in input_file:
line = line.strip()
output_line = delimiter.join(reversed(line.split(delimiter)))
output_file.write(line+"\n")
output_file.write(output_line+"\n")
swap()
Riffing on #DSM:
with open(infilename) as infile, open(outfilename, 'w') as outfile:
lines = [line.rstrip() for line in infile]
outfile.write("\n".join("%s\n%s%s%s" (line, y, sep, x)
for line in lines
for x, y in line.split(sep)) + "\n")
lines could also be a generator statement instead of a list comprehension:
lines = (line.rstrip() for line in infile)
Later: I did not realize until now that OP wanted the original line followed by the reversed line. Adjusted accordingly.