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())))
Related
Read the first two lines from a text file named "file1.txt" Write the two lines read from "file1.txt" to a new file "file2.txt"
a_file = open("file1.txt", "r")
number_of_lines = 2
with open("file2.txt", "w") as new_file:
for i in range(number_of_lines):
line = a_file.readline()
new_file.write(line)
a_file.close()
I'm sure there is a neater solution out there somewhere but this will work!
Hope it helps you :)
Write a Python program to
Read the first two lines from a text file named "file1.txt"
Write the two lines read from "file1.txt" to a new file called
"file2.txt"
Read "file2.txt" and Print the contents
fhandle1 = open("file1.txt","r")
fhandle2 = open("file2.txt","w")
str = fhandle1.readline()
fhandle2.write(str)
str = fhandle1.readline()
fhandle2.write(str)
fhandle1.close()
fhandle2.close()
fhandle3 = open("file2.txt")
print(fhandle3.read())
fhandle3.close()
For 2 lines:
with open("file1.txt", "r") as r:
with open("file2.txt", "w") as w:
w.write(r.readline() + r.readline())
Each time r.readline() is called, it goes to the next line. So if you wanted to read n lines; use:
Note that .readline() + r.readline() is only 2 seperate lines if there is a new line (\n) at the end of the first line
with open("file1.txt", "r") as r:
with open("file2.txt", "w") as w:
# Change 2 to number of lines to read
for i in range(2):
w.write(r.readline())
f1=open("file1.txt","r")
f2=open("file2.txt","w")
fcontent=f1.readline()
f2.write(fcontent)
fcontent=f1.readline()
f2.write(fcontent)
f1.close()
f2.close()
f1 = open("file1.txt","r")
f2 = open("file2.txt","w")
str = f1.readline()
f2.write(str)
str = f1.readline()
f2.write(str)
f1.close()
f2.close()
f3 = open("file2.txt")
print(f3.read())
f3.close()
fhandle1 = open("file1.txt")
fhandle2 = open("file2.txt","w")
fcontents = fhandle1.readline()
fhandle2.write(fcontents)
fcontents = fhandle1.readline()
fhandle2.write(fcontents)
fhandle1.close()
fhandle2.close()
fhandle3 = open("file2.txt")
print(fhandle3.read())
fhandle3.close()
fhandle1 = open("file1.txt","r")
l1 = fhandle1.readline()
l2 = fhandle1.readline()
fhandle2 = open("file2.txt","w")
fhandle2.write(l1)
fhandle2.write(l2)
fhandle2 = open("file2.txt")
print(fhandle2.read())
fhandle2.close()
I usually work in bash so i'm very new to this frightening world of python.
I am attempting to search a file for a string then insert text above the "First occurrence" of that string with empty line between.
The file to be edited would look like this:
Name:
Billy
Thorton
Billy
Thorton
I am trying to insert "Bob" above "Thorton" with the empty lines between like this:
Name:
Billy
Bob
Thorton
Billy
Thorton
This is the Python i have so far.
contents = "Bob"
f = open("file", "w")
contents = "".join(contents)
f.write(contents)
f.close()
This does not search for the string and it replaces the whole file.
A working example in bash would be:
sed -i '0,/Thorton/s//Bob\n\n&/' file
A common way to do so in Python would be to open the file, iterate over it line by line and prepare the results, then write the results to the file.
res = ""
with open("test.txt", "r") as f:
data = f.readlines() # Read the file line by line
found = False
for line in data:
if "Thorton" in line and not found:
res += "Bob\n\n" # Insert Bob if needed
found = True
res += line # Insert the line we just read
with open("test.txt", "w") as f:
f.write(res) # Write the answer in the same file
You could use str.split() to get each item into a list then use list.index() to get the position of "Thorton" to insert from then str.join() to get them back into writable form:
with open('filename.txt', 'r') as infile:
data = infile.read().split()
data.insert(data.index('Thorton'), 'Bob')
with open('filename.txt', 'w') as outfile:
outfile.write('\n\n'.join(data))
you could do
searchedName = "Thorton"
addedName= "Bob"
f = open("file", "w")
content = f.readlines()
index = content.index(searchedName + '\n')
contents = content.insert(index , addedName + '\n')
contents = "".join(contents)
f.write(contents)
f.close()
I am working with an output log file that has 12 thousand lines of code, most of which include something that looks like this:
"760.0132 EXP window1: blendMode = 'avg'"
My goal is to entirely remove any line that has "EXP window1: blendMode = 'avg'". I can remove that text bit from all of the lines where it is found, but not the number. This is the code I have used to delete the text bits (borrowed from another stack overflow question/answer):
infile = "01_Day1_run1.txt"
outfile = "01_Day1_run1_cleaned.txt"
delete_list = [" EXP window1: blendMode = 'avg'"]
fin = open(infile)
fout = open(outfile, "w+")
for line in fin:
for word in delete_list:
line = line.replace(word, "")
fout.write(line)
fin.close()
fout.close()
I was hoping that I would be able to add something like
delete_list = ["1**.**** EXP window1: blendMode = 'avg'"]
in order to delete any number that includes all of the text, and also any number in that line, but it does not seem to work. Any advice on how to best clean up the log file would be greatly appreciated.
Thanks very much,
Simon
Why do you want to do this using Python? You can do this with a simple grep -v or findstr /V, as in following example:
Prompt>grep -v "blendmode" input.txt >output.txt
infile = "01_Day1_run1.txt"
outfile = "01_Day1_run1_cleaned.txt"
delete_list = [" EXP window1: blendMode = 'avg'"]
fin = open(infile)
fout = open(outfile, "a")
for line in fin:
for word in delete_list:
if word in line:
wordCheck = False
break
else:
wordCheck = True
if wordCheck:
fout.write(line)
fin.close()
fout.close()
Maybe cleaner:
with open("01_Day1_run1.txt", "r") as infile, open("01_Day1_run1_cleaned.txt", "a") as outfile:
for line in infile:
if not any(filter in line for filter in delete_list ):
outfile.write(line)
infile = "01_Day1_run1.txt"
outfile = "01_Day1_run1_cleaned.txt"
delete_string = "EXP window1: blendMode = 'avg'"
fin = open(infile)
fout = open(outfile, "a")
for line in fin.readLines():
if delete_list not in line:
fout.write(line)
fin.close()
fout.close()
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()
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.