How to write Python Shell to an output text file? - python

I need to write my Python shell to an output text file. I have some of it written into an output text file but all I need is to now add the number of lines and numbers in each line to my output text file.
I have tried to add another for loop outside the for loop. I've tried putting it inside the for loop and it was just complicated.
Text file list of numbers:
1.0, 1.12, 1.123
1.0,1.12,1.123
1
Code:
import re
index = 0
comma_string = ', '
outfile = "output2.txt"
wp_string = " White Space Detected"
tab_string = " tab detected"
mc_string = " Missing carriage return"
ne_string = " No Error"
baconFile = open(outfile,"wt")
with open("Version2_file.txt", 'r') as f:
for line in f:
flag = 0
carrera = ""
index = index +1
print("Line {}: ".format(index))
baconFile.write("Line {}: ".format(index))
if " " in line: #checking for whitespace
carrera = carrera + wp_string + comma_string + carrera
flag = 1
a = 1
if "\t" in line: #checking for tabs return
carrera = carrera + tab_string + comma_string + carrera
flag = 1
if '\n' not in line:
carrera = carrera + mc_string + ne_string + carrera
flag = 1
if flag == 0: #checking if no error is true by setting flag equal to zero
carrera = ne_string
print('\t'.join(str(len(g)) for g in re.findall(r'\d+\.?(\d+)?', line )))
print (carrera)
baconFile.write('\t'.join(str(len(g)) for g in re.findall(r'\d+\.?(\d+)?', line ) ))
baconFile.write(carrera + "\n")
with open("Version2_file.txt", 'r') as f:
content = f.readlines()
print('Number of Lines: {}'.format(len(content)))
for i in range(len(content)):
print('Numbers in Line {}: {}'.format(i+1, len(content[i].split(','))))
baconFile.write('Number of lines: {}'.format(len(content)))
baconFile.write('Numbers in Line {}: {}'.format(i+1, len(content[i].split(','))))
baconFile.close()
Expected to write in output file:
Line 1: 1 2 3 Tab detected, whitespace detected
Line 2: 1 2 3 No error
Line 3: 1 Missing carriage return No error
Number of Lines: 3
Numbers in Line 1: 3
Numbers in Line 2: 3
Numbers in Line 3: 1
Actual from output file:
Line 1: 1 3 2White Space Detected, tab detected, White Space Detected,
Line 2: 1 3 2No Error
Line 3: 0Missing carriage returnNo Error
Number of lines: 3Numbers in Line 1: 3Number of lines: 3Numbers in Line 2: 3Numb

You have closed baconFile in the first open block, but do not open it again in the second open block. Additionally, you never write to baconFile in the second open block, which makes sense considering you've not opened it there, but then you can't expect to have written to it. It seems you simply forgot to add some write statements. Perhaps you confused write with print. Add those write statements in and you should be golden.
baconFile = open(outfile,"wt")
with open("Version2_file.txt", 'r') as f:
for line in f:
# ... line processing ...
baconFile.write(...) # line format info here
# baconFile.close() ## <-- move this
with open("Version2_file.txt", 'r') as f:
content = f.readlines()
baconFile.write(...) # number of lines info here
for i in range(len(content)):
baconFile.write(...) # numbers in each line info here
baconFile.close() # <-- over here

Here's a useful trick you can use to make print statements send their output to a specified file instead of the screen (i.e. stdout):
from contextlib import contextmanager
import os
import sys
#contextmanager
def redirect_stdout(target_file):
save_stdout = sys.stdout
sys.stdout = target_file
yield
sys.stdout = save_stdout
# Sample usage
with open('output2.txt', 'wt') as target_file:
with redirect_stdout(target_file):
print 'hello world'
print 'testing', (1, 2, 3)
print 'done' # Won't be redirected.
Contents of output2.txt file after running the above:
hello world
testing (1, 2, 3)

Related

Python removing multiple lines beginning using a list of strings

I am trying to remove multiple lines from a file where the lines begin with a specified string.
I've tried using a list as below but the lines are written to the file the number of times equal to the items in the list. Some of the lines are removed some are not I'm pretty sure that it is due to not reading the next line at the correct time
trunklog = open('TrunkCleanedDaily.csv', 'r')
fh = open("TCDailyFinal.csv", "w")
firstletter = ['Queue,Completed', 'Outbound,', 'Red_Team_DM,', 'Sunshine,', 'Agent,','Disposition,', 'Unknown,']
while True:
line = trunklog.readline()
if not line:
break;
for i in firstletter:
if line.startswith(i):
print('del ' + line, end='')
# line = trunklog.readline()
else:
fh.write(line)
print('keep ' + line,end='')
line = trunklog.readline()
Any help setting me straight about this is appreciated.
Some of the content I am trying to remove:
Queue,Completed,Abandons,Exits,Unique,Completed %,Not Completed %,Total Calls,
Green_Team_AMOne,93,0,0,0,100.00%,0.00%,8.04%,
Green_Team_DM,11,0,0,0,100.00%,0.00%,0.95%,
Green_Team_IVR,19,0,0,0,100.00%,0.00%,1.64%,
Outbound,846,131,0,0,86.59%,13.41%,84.44%,
Red_Team_AMOne,45,0,0,0,100.00%,0.00%,3.89%,
Red_Team_DM,3,0,0,0,100.00%,0.00%,0.26%,
Red_Team_IVR,5,0,0,0,100.00%,0.00%,0.43%,
Sunshine,4,0,0,0,100.00%,0.00%,0.35%,
Queue,Total Call Time,Average Call Time,Average Hold Time,Call Time %,None,
Green_Team_AMOne,32:29:06,20:57,00:10,42.92%,None,
Green_Team_DM,2:41:35,14:41,00:16,3.56%,None,
Green_Team_IVR,1:47:12,05:38,00:19,2.36%,None,
Try below code:
trunklog = open('TrunkCleanedDaily.csv', 'r')
fh = open("TCDailyFinal.csv", "w")
firstletter = ['Queue,Completed', 'Outbound,', 'Red_Team_DM,', 'Sunshine,', 'Agent,', 'Disposition,', 'Unknown,']
for line in trunklog:
cnt=0
for i in firstletter:
if line.startswith(i):
print('del ' + line, end='')
cnt=1
if not cnt:
fh.write(line)
print('keep ' + line, end='')
I have modified your code a little bit.
And added a variable 'cnt', which will be 1, if first word is in firstletter list.
If cnt=0, then it will write line to the new file.
You just have to left intend else statement for for loop and add break if you have to delete a line.
trunklog = open('TrunkCleanedDaily.csv', 'r')
fh = open("TCDailyFinal.csv", "w")
firstletter = ['Queue,Completed', 'Outbound,', 'Red_Team_DM,', 'Sunshine,', 'Agent,','Disposition,', 'Unknown,']
while True:
line = trunklog.readline()
if not line:
break;
for i in firstletter:
if line.startswith(i):
print('del ' + line, end='')
break
else:
fh.write(line)
print('keep ' + line,end='')
Output file
Green_Team_AMOne,93,0,0,0,100.00%,0.00%,8.04%,
Green_Team_DM,11,0,0,0,100.00%,0.00%,0.95%,
Green_Team_IVR,19,0,0,0,100.00%,0.00%,1.64%,
Red_Team_AMOne,45,0,0,0,100.00%,0.00%,3.89%,
Red_Team_IVR,5,0,0,0,100.00%,0.00%,0.43%,
Queue,Total Call Time,Average Call Time,Average Hold Time,Call Time %,None,
Green_Team_AMOne,32:29:06,20:57,00:10,42.92%,None,
Green_Team_DM,2:41:35,14:41,00:16,3.56%,None,
Green_Team_IVR,1:47:12,05:38,00:19,2.36%,None,

Python write blank

I'm trying to make a program which would replace tags in a markdown file (.md) as follow :
If it's an opening $ tag, replace it by \(, if it's a closing $ tag, replace it by \), copy every other characters.
Unfortunately, when I try it, the file written is really strange. Some lines are copied but others aren't. First, the first and last line of every of my test files weren't copied. Other lines in the middle weren't as well. Same text on different line are not both copied.
Here is my program :
import os
def conv1(path):
"""convert $$ tags to \( \)"""
file = open(path, mode ='r') # open lesson with $ (.md)
new = open(path + '.tmp', mode = 'w') # open blank file
test = 0
for lines in file:
line = file.readline()
i = 0
length = len(line)
while i < length:
if line[i] == '$':
if test % 2 == 0: # replace opening tag
line = line[:i] + '\(' + line [i + 1:]
elif test % 2 == 1: # replace closing tag
line = line[:i] + '\)' + line [i + 1:]
test +=1
i += 2
length += 1
else :
i += 1
new.write(line + '\n')
file.close()
new.close()
os.rename(str(path) + '.tmp', str(path))
print('Done!')
Do you have any idea how to fix my issue?
Thanks in advance
EloiLmr
These line are causing every other line to be skipped:
for lines in file:
line = file.readline()
Calling file.readline() unnecessarily advances the file pointer by one line. It's enough to iterate over the file:
for line in file:
...

Python - Add string to beginning of file in loop

I have this code:
read = open('file.py', 'r+')
intent = 0
for line in read:
if '{' in line:
intent += 1
#print intent, ", up"
if '}' in line:
#intent -= 1
print intent, ", down"
if " " in line:
print "tab"
I want to add
"\t" * intent
to the beginning every line.
How can I do that?
You can open, read, edit, then overwrite the file to acheive this effect, as follows:
file=open('file.py', 'r')
lines=file.read().split('\n')
file.close()
output=[('\t'*intent)+i for i in lines]
file=open('file.py', 'w')
file.writelines(output)
file.close()
Is this what you're trying to do?
read = open('file.py', 'r+')
indent = 0
new_lines = []
for line in read:
new_lines.append(("\t" * indent) + line)
if '{' in line:
indent += 1
if '}' in line:
indent -= 1
print("".join(new_lines))

Python string compare and replace

I have a .txt with:
#Date 111111:UhUidsiIds
#Name Sebastian-Forset
#Date 222222:UdfasdUDsa
#Name Sebastian_Forset2
#Date 333333:UDsafduD
#Name Solaris Mage
#Date 444444:Ghdsasra
#Name Marge S
and a file whith:
#Name Sebastian Forset
#Date 191020
#Name Sebastian Forset2
#Date 201020
#Date Homer S
#Date 281902
The names are the same, with some differences of characters (spaces, -, _ etc.)
I would copy the numbers of the second file to the first file in order to have a final file txt with:
#Name Sebastian Forset
#Date 191020:UhUidsiIds
#Name Sebastian Forset2
#Date 201020:UdfasdUDsa
#Name Solaris Mage
#Date 281902:UDsafduD
#Name Marge S
#Date 444444:Ghdsasra
This is my code, but merge the file, copy only same name
def isInFile(l, f):
with open(f, 'r') as f2:
for line in f2:
if l == line:
return True
return False
def similitudes(file1, file2):
same = 0
data = ''
copy = False
with open(file1, 'r') as f1:
for line in f1:
if copy == True:
data += line
if line == '\n' or line[0:6] != '#Name ':
copy = False
if (line[0:6] == '#Name ') or line[0:6] == '#Date ':
print line
if isInFile(line, file2) == True:
copy = True
data += line
print "true"
else:
print "ok"
same += 1
return data
def main(argv=2):
print (sys.argv[1])
print (sys.argv[2])
if argv == 2:
out = open('final.txt', 'w')
data = (
similitudes(sys.argv[1], sys.argv[2]) + '\n'
)
out.write(data)
out.close()
else:
print ("This program need 2 files")
exit (0)
return 0
if __name__ == '__main__':
status = main()
sys.exit(status)
First, list out the characters that will differ. Let's say "-" , "_" and " ".
Now split the two strings using these delimiters. you can use "re" package in python.
>>> a='Mr-Sebastian_Forset '
>>> import re
>>> re.split('- |_ | ',a)
['Mr', 'Sebastian', 'Forset']
If the resultant lists for the two strings are equal, paste the number in second file in first one.
You can use the same delimiter concept to split the number and paste it in other file.
Adding another answer, which will points out the bug in your code
Coming to the following piece of code
if (line[0:6] == '#Name ') or line[0:6] == '#Date ':
print line
if isInFile(line, file2) == True:
copy = True
data += line
Here, you are checking If your line starts with either "#Name " or "#Date ", and calling isInFile() method with line and file2 as arguments.
This is the first issue, there is no use of sending just one line that starts with "#Name " in your case.
If the current line starts with "#Date ", send the previous line and file as arguments to this method.
And second Issue is with the isInFile() definition, which is doing effectively nothing.
if l == line:
return true
You are just checking if two lines in file1 and file2 are same and if yes, you writing this line in sysout.
So, your program will just print the common lines between file1 and file2.
Modified code should like the below one:
def isInFile(l, f):
line_found = false
required_line = null
with open(f, 'r') as f2:
for line in f2:
if line_found:
required_line = line
break
elif l == line:
line_found = true
return (line_found, required_line)
def similitudes(file1, file2):
same = 0
data = ''
copy = False
previous_line = null
with open(file1, 'r') as f1:
for line in f1:
if copy == True:
data += line
if line == '\n' or line[0:6] != '#Name ':
copy = False
if (line[0:6] == '#Name '):
print line
previous_line = line
elif line[0:6] == '#Date ':
print line
file2_line_info = isInFile(previous_line, file2)
if file2_line_info[0] == True:
copy = True
data += file2_line_info[1]
print "true"
return data
def main(argv=2):
print (sys.argv[1])
print (sys.argv[2])
if argv == 2:
out = open('final.txt', 'w')
data = (
similitudes(sys.argv[1], sys.argv[2]) + '\n'
)
out.write(data)
out.close()
else:
print ("This program need 2 files")
exit (0)
return 0
if __name__ == '__main__':
status = main()
sys.exit(status)
Note: This is not the pythonic way of doing things. As I have mentioned in the above answer https://stackoverflow.com/a/34696778/3534696 use "re" module and solve the problem efficiently.
Read the first file into a dictionary, using maketrans/translate to clean up the name.
Using zip(file, file) to read 2 lines of the file at a time makes it much easier to handle.
And using .split(' ', 1)[1] to get rid of the first column.
And .strip() to get rid of any surrounding whitespace (i.e. \n)
Then you can read the second file updating the dictionary.
In Python3 this looks like:
>>> punc = str.maketrans('_-', ' ') # import string & string.maketrans() in Py2
>>> with open(filename1) as file1, open(filename2) as file2:
... data = {name.split(' ', 1)[1].strip().translate(punc):
... date.split(' ', 1)[1].strip().split(':')
... for name, date in zip(file1, file1)}
... for n, d in zip(file2, file2):
... data[n.split(' ', 1)[1].strip()][0] = d.split(' ', 1)[1].strip()
>>> data
{'Marge S': ['444444', 'Ghdsasra'],
'Sebastian Forset': ['191020', 'UhUidsiIds'],
'Sebastian Forset2': ['201020', 'UdfasdUDsa'],
'Solaris Mage': ['281902', 'UDsafduD']}
After that it is just a matter of writing the dictionary out to a new file.
>>> with open(<output>, 'w+') as output:
... for name, date in data.items():
... output.write('#Name {}\n'.format(name))
... output.write('#Date {}:{}\n'.format(*date))
Note: I had to change 'Homer S' to 'Solaris Mage' in the second file to get the stated output.

Script is skipping function

I have no idea whats going on with this code, for some reason it seems to just skip the function entirely.
try:
readHandle = open(fileName, 'r')
except IOError, ioe:
print "Cannot open file: ", fileName,"\n"
print "%s" %ioe
raise
lines = readHandle.readlines()
lineNum = 1
#read file line by line
for line in lines:
if line.startswith(':'):
#remove : from line
bits0 = line.partition(':')
#remove \n newlines
bits1 = bits0[2].partition('\n')
#split in to an array using , as delimiter
bits2 = bits1[0].split(',')
DrvrNum = bits2[0]
DrvrNam = bits2[1]
# Debug
if DBUG == 1:
print "DrvrNum and DrvrNam variable values"
print DrvrNum, DrvrNam
crcDrvr(DrvrNum, DrvrNam)
elif line.startswith('#'):
#Comment line
pass
elif line.startswith('Ss'):
#Crc line
pass
elif line.startswith('Zz'):
#end of file
pass
else:
print '\nError: line', lineNum , 'is an illegal entry'
print '\nPlease Check'
sys,exit(0)
lineNum = lineNum + 1
This is the function that is being skipped:
def crcDrvr(number,name):
convNum = int(number,16)
convNam = ''
for char in name:
hexChar = char.encode("hex")
print hexChar
can anyone tell me where I've gone wrong to cause my code to skip?
Sample data:
#DrvrDB
#
#
#
Ss1234
:744,Bob Hope
:747,Testy Tester
:777,Extra Guy
:0,dummy
Zz
#Driver#,DriverName
#end of file padding 1
I figured it out, some genius create the function crcDrvr twice with only a variable declaration so it must have been hitting that one
– Jim

Categories

Resources