Here is sample code to show that I'm running these commands again. I'm guessing this is the wrong way to do this.
for filename in os.listdir(path):
with open(os.path.join(path,filename), "r") as infile:
for line in infile:
if re.search(r"\b1000\b", line, flags=re.IGNORECASE):
count1 += 1
fh.write("{}, ".format(count1))
for filename in os.listdir(path):
with open(os.path.join(path,filename), "r") as infile:
for line in infile:
if re.search(r"\b10G\b", line, flags=re.IGNORECASE):
count2 += 1
fh.write("{}, ".format(count2))
I hope I have understood your idea correctly. I suggest you could do the following.
fh = open("my.csv", "w+")
path = 'my path'
for filename in os.listdir(path):
with open(os.path.join(path,filename), "r") as infile:
count1 = 0
count2 = 0
for line in infile:
if re.search(r"\b10G\b", line, flags=re.IGNORECASE):
count1 += 1
if re.search(r"\b10G\b", line, flags=re.IGNORECASE):
count2 += 1
# write the count value to the csv after reading the file
fh.write("Count1: {} Count2: {}\n".format(count1, count2))
# close the file reader - otherwise there won't be any content in the csv
fh.close()
You can do :
fh = open("output.csv", "w")
for filename in os.listdir(path):
with open(os.path.join(path,filename), "r") as infile:
for line in infile:
if re.search(r"\b10G\b", line, flags=re.IGNORECASE):
count1 += 1
fh.write("{}, ".format(count1))
Related
I have a series of files and I have to separate and display part of the text
my code is :
path = 'C:\\Bot\\*.log'
files = glob.glob(path)
nlines = 0
for name in files:
try:
with open(name) as f:
for line in f :
nlines += 1
if (line.find("Total") >= 0):
print(line)
I need a text that is saved in the file after the line number is obtained.
With the above code, I have access to the line number but I do not have access to some subsequent lines
How to access the next line value??
path = 'C:\\Bot\\*.log'
files = glob.glob(path)
nlines = 0
for name in files:
try:
with open(name) as f:
for line in f:
nlines += 1
if (line.find("Total") >= 0):
print(next(f))
I think it is a better solution to this problem
Use next() to read:
path = 'C:\\Bot\\*.log'
files = glob.glob(path)
nlines = 0
for name in files:
try:
with open(name) as f:
for line in f:
nlines += 1
if (line.find("Total") >= 0):
for i in range(6):
print(next(f))
I Would like to remove lines 3 to 15 from file 'Database.csv'. Please see my code below, this code is works only for one line i added range, but is didn't work. ;/
filename = 'Database.csv'
line_to_delete = [3:15]
initial_line = 1
file_lines = {}
with open(filename) as f:
content = f.readlines()
for line in content:
file_lines[initial_line] = line.strip()
initial_line += 1
f = open(filename, "w")
for line_number, line_content in file_lines.items():
if line_number != line_to_delete:
f.write('{}\n'.format(line_content))
f.close()
print('Deleted line: {}'.format(line_to_delete))
You can use csv library for doing this.
import csv
file=open("Database.csv",'rb')
final_file=open("Database_edited",'wb')
writer=csv.writer(final_file)
line_no=1 # for knowing the line number
for row in csv.reader(file):
if(line_no<=3 or line_no>=15):
writer.writerow(row)
line_no=line_no+1
file.close()
final_file.close()
This way Database_edited will have your required file
Here's an easy way to do using the csv module and the range() function:
mport csv
filename = 'Database.csv'
startline, endline = 3, 15 # Inclusive.
with open(filename, 'r', newline='') as f:
content = [row for i,row in enumerate(csv.reader(f), 1)
if i not in range(startline, endline+1)]
filename2 = 'Database2.csv' # Write to different file for testing.
with open(filename2, 'w', newline='') as f:
csv.writer(f).writerows(content)
print('lines deleted')
#Tested code on Python 3.6
import csv
filename = r'C:\Users\91852\Desktop\New folder (2)\lines.csv'
start_skip_row = 3
stop_skip_row = 13
def readCSVFile(file_name: str, start: int, stop: int) -> list:
with open(file_name, 'r') as file_content:
data_after_deletion = [line.replace('\n', '') for idx, line in enumerate(file_content) if
not start - 1 <= idx < stop]
print(data_after_deletion)
return data_after_deletion
def writeCSVFile(data: list) -> None:
with open('file_after_deletion', 'w', newline='') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(data)
data_after_deletion = readCSVFile(filename, start_skip_row, stop_skip_row)
writeCSVFile(data_after_deletion)
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())
I have a large csv file for which i need to split the file. I have
managed to split the file using the below python code:
import csv
divisor = 500000
outfileno = 1 outfile = None
with open('file_temp.txt', 'r') as infile:
for index, row in enumerate(csv.reader(infile)):
if index % divisor == 0:
if outfile is not None:
outfile.close()
outfilename = 'big-{}.csv'.format(outfileno)
outfile = open(outfilename, 'w')
outfileno += 1
writer = csv.writer(outfile)
writer.writerow(row)
The problem i'm facing is that the file header is not getting copied
to the rest of the files. Can you please let me know how can i modify
my code to add the headers in the different splitted files.
You just need to cache the header row and then write it out for each CSV file, something like:
import csv
divisor = 500000
outfileno = 1
outfile = None
try:
with open('file_temp.txt', 'r') as infile:
infile_iter = csv.reader(infile)
header = next(infile_iter)
for index, row in enumerate(infile_iter):
if index % divisor == 0:
if outfile is not None:
outfile.close()
outfilename = 'big-{}.csv'.format(outfileno)
outfile = open(outfilename, 'w')
outfileno += 1
writer = csv.writer(outfile)
writer.writerow(header)
writer.writerow(row)
finally:
# Don't forget to close the last file
if outfile is not None:
outfile.close()
Since you're only working with lines, you don't really need to use the CSV module, here's a version that works without it:
divisor = 500000
outfileno = 1
outfile = None
try:
with open('file_temp.txt', 'r') as infile:
header = next(infile)
for index, row in enumerate(infile):
if index % divisor == 0:
if outfile is not None:
outfile.close()
outfilename = 'big-{}.csv'.format(outfileno)
outfile = open(outfilename, 'w')
outfileno += 1
outfile.write(header)
outfile.write(row)
finally:
# Don't forget to close the last file
if outfile is not None:
outfile.close()
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())))