How to extract data(time) from .txt file? - python-2.7

I have .txt file, from txt file i want all the timings to check the performance. How to get the all times in to new list?
below is my .txt file:
03-21 12:09:34.123 application open
03-21 12:09:35.122 date
03-21 12:09:36.124 completed
03-21 12:09:37.125 None
Below is what i tried:
def memory(self):
self.empty = []
time_x = []
heap_y = [0,20,40,60,80,100,120]
pattren =re.compile(r'^(([01]\d|2[0-3]):([0-5]\d)|24:00)$')
with open("C:\\Sakurai_Robot\\testlogs\\logcat_105010176.log", "r") as gummi:
for i in gummi.readlines():
if pattren.search(i) !=None:
self.empty.append(i.rstrip('\n'))
print self.empty
I want only time like:
12:09:34
12:09:35
12:09:36
12:09:37
But i am not able to get.is there any way we will get all the times into new list?

You could do it completely without using regular expression, too:
timestamps = []
with open("C:\\Sakurai_Robot\\testlogs\\logcat_105010176.log", "r") as gummi:
for i in gummi.readlines():
timestamps.append(i.split(" ")[1].split(".")[0])
print timestamps

This code here should work through your problem.
import re
output = []
p = re.compile('\d\d:\d\d:\d\d')
with open("path", "r") as fle:
for i in fle.readlines():
lst = p.findall(p)
for match in lst:
output.append(match)
for a in output:
print(a)
When run against your input, the output is as follows:
12:09:34
12:09:35
12:09:36
12:09:37

Related

rstrip, split and sort a list from input text file

I am new with python. I am trying to rstrip space, split and append the list into words and than sort by alphabetical order. I don’t what I am doing wrong.
fname = input("Enter file name: ")
fh = open(fname)
lst = list(fh)
for line in lst:
line = line.rstrip()
y = line.split()
i = lst.append()
k = y.sort()
print y
I have been able to fix my code and the expected result output.
This is what I was hoping to code:
name = input('Enter file: ')
handle = open(name, 'r')
wordlist = list()
for line in handle:
words = line.split()
for word in words:
if word in wordlist: continue
wordlist.append(word)
wordlist.sort()
print(wordlist)
If you are using python 2.7, I believe you need to use raw_input() in Python 3.X is correct to use input(). Also, you are not using correctly append(), Append is a method used for lists.
fname = raw_input("Enter filename: ") # Stores the filename given by the user input
fh = open(fname,"r") # Here we are adding 'r' as the file is opened as read mode
lines = fh.readlines() # This will create a list of the lines from the file
# Sort the lines alphabetically
lines.sort()
# Rstrip each line of the lines liss
y = [l.rstrip() for l in lines]
# Print out the result
print y

How to get a list of strings to print out vertically in a text file?

I have some data that I've pulled from a website. This is the code I used to grab it (my actual code is much longer but I think this about sums it up).
lid_restrict_save = []
for t in range(10000,10020):
address = 'http://www.tspc.oregon.gov/lookup_application/' + lines2[t]
page = requests.get(address)
tree = html.fromstring(page.text)
#District Restriction
dist_restrict = tree.xpath('//tr[11]//text()')
if u"District Restriction" in dist_restrict:
lid_restrict_save.append(id2)
I'm trying to export this list:
print lid_restrict_save
[['5656966VP65', '5656966RR68', '56569659965', '56569658964']]
to a text file.
f = open('dis_restrict_no_uniqDOB2.txt', 'r+')
for j in range(0,len(lid_restrict_save)):
s = ( (unicode(lid_restrict_save[j]).encode('utf-8') + ' \n' ))
f.write(s)
f.close()
I want the text to come out looking like this:
5656966VP65
5656966RR68
56569659965
56569658964
This code worked but only when I started the range from 0.
f = open('dis_restrict.txt', 'r+')
for j in range(0,len(ldob_restrict)):
f.write( ldob_restrict[j].encode("utf-8") + ' \n' )
f.close()
When I've tried changing the code I keep getting this error:
"AttributeError: 'list' object has no attribute 'encode'."
I've tried the suggestions from here, here, and here but to no avail.
If anyone has any hints it would be greatly appreciated.
lid_restrict_save is a nested list so you can't encode the first element because it is not a string.
You could write to the txt file using this:
lid_restrict_save = [['5656966VP65', '5656966RR68', '56569659965', '56569658964']]
lid_restrict_save = lid_restrict_save[0] # remove the outer list
with open('dis_restrict.txt', 'r+') as f:
for i in lid_restrict_save:
f.write(str(i) + '\n')

How to insert two file.txt into one file

I have this function that takes two input .txt file, delete the punctuation mark, and adds the sentence pos or neg.
I would like the content of these fle converted to lowercase
and then these two files merged into a single file name union.txt
But my code does not work
def extractor (feature_select):
posFeatures = []
negFeatures = []
with open('positive.txt', 'r') as posSentences:
for i in posSentences:
posWords = re.findall(r"[\w']+|[(,.;:*##/?!$&)]", i.rstrip())
posWords = [feature_select(posWords), 'pos']
posFeatures.append(posWords)
with open('negative.txt', 'r') as negSentences:
for i in negSentences:
negWords = re.findall(r"[\w']+|[(,.;:*##/?!$&)]", i.rstrip())
negWords = [feature_select(negWords), 'neg']
negFeatures.append(negWords)
return posFeature, negFeature
filenames = [posFeature, negFeature]
with open('union.txt', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())
Actually you are trying to open the files with names from the contents of the two files. fname holds the contents read from the input files.
filenames = [posFeature, negFeature]
with open('union.txt', 'w') as outfile :
for i in filenames : #refers to posFeature or negFeature which is a list
for j in i: #this loop reads each sentence from the list i
outfile.write(j) #it writes the sentence into outfile
No need to read back the contents already read and appended in posFeature and negFeature. Above code will directly write the contents in the list filenames and now your two files are merged.

List to Text or Replace word in Text File

So I have a text file that contains:
Toy_One Ordered
Toy_Two Ordered
Toy_Three Ordered
I want to remove the " Ordered" from the text file. I figured making a list and rewriting to the file would be simple, or even a replace but clearly not. So far, I have been only able to rewrite Toy_Three.
Infile = (src.txt, "r")
g = list(Infile)
stripg = [mark.strip() for mark in g]
for line in stripg:
entry = line.split(" Ordered")
NewList = list(entry[0])
InOutFile = (src, "w")
for final in NewList:
InOutFile.write(final)
My outcome is just Toy_Three
I've also tried "pickle" but with no success.
Any help would be appreciated, thanks.
Nevermind, got it:
Infile = (src.txt, "r")
magic = Infile.readlines()
delete = [" Ordered"]
Outfile = (src.txt, "w")
for line in magic:
for word in delte:
line = line.replace(word,"")
Outfile.write(line)

Python: program not writing to file after breaking the loop

I am fairly new to Python, and I am messing around with code and I am wondering why my code, when the for loop is broken, does not write to the file.
But, when I do not break the for loop, it writes to the file over and over again with no end, deleting the previous content (I only want it to loop through once and get the data on the first iteration of it):
def runExtract(self):
with open(self.fileIn) as f:
content = f.readlines()
#query = f.readlines()
for query in content:
result=google_search.SearchEngine.search(query, 3)
"""junk"""
file1=open(self.fileOutGoog,'r+b')
"""junk"""
for k in result:
#file.write(str(k.name)+"\t")
file1.write(str(k.link)+"\t")
#file.write(str(k.description)+"\t")
file1.write("\n")
#file=open(self.fileOutGoog,'rb')
file1.flush() """writes to file, deleting the '/url?q=' from html"""
file1.close()
file1 = open(self.fileOutGoog,'r')
filedata = file1.read()
file1.close()
newdata = filedata.replace('/url?q=','')
file1 = open(self.fileOutGoog,'w')
file1.write(newdata)
file1.flush()
file1.close()
result=bing_search.SearchEngine.search(query, 3)
"""junk"""
file2=open(self.fileOutBing,'w')
for k in result:
#file.write(str(k.name)+"\t")
file2.write(str(k.link)+"\t")
# file.write(str(k.description)+"\t")
file2.write("\n")
file2.flush()
file2.close()
break
print("done - check dir for results")
f.flush()
f.close()
I have been trying to fix this for two days straight, but I have not succeeded.