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,
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))
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.
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