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.
Related
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
The basic outline of this problem is to read the file, look for integers using the re.findall(), looking for a regular expression of [0-9]+ and then converting the extracted strings to integers and summing up the integers.
I am finding trouble in appending the list. From my below code, it is just appending the first(0) index of the line. Please help me. Thank you.
import re
hand = open ('a.txt')
lst = list()
for line in hand:
line = line.rstrip()
stuff = re.findall('[0-9]+', line)
if len(stuff)!= 1 : continue
num = int (stuff[0])
lst.append(num)
print sum(lst)
import re
ls=[];
text=open('C:/Users/pvkpu/Desktop/py4e/file1.txt');
for line in text:
line=line.rstrip();
l=re.findall('[0-9]+',line);
if len(l)==0:
continue
ls+=l
for i in range(len(ls)):
ls[i]=int(ls[i]);
print(sum(ls));
Great, thank you for including the whole txt file! Your main problem was in the if len(stuff)... line which was skipping if stuff had zero things in it and when it had 2,3 and so on. You were only keeping stuff lists of length 1. I put comments in the code but please ask any questions if something is unclear.
import re
hand = open ('a.txt')
str_num_lst = list()
for line in hand:
line = line.rstrip()
stuff = re.findall('[0-9]+', line)
#If we didn't find anything on this line then continue
if len(stuff) == 0: continue
#if len(stuff)!= 1: continue #<-- This line was wrong as it skip lists with more than 1 element
#If we did find something, stuff will be a list of string:
#(i.e. stuff = ['9607', '4292', '4498'] or stuff = ['4563'])
#For now lets just add this list onto our str_num_list
#without worrying about converting to int.
#We use '+=' instead of 'append' since both stuff and str_num_lst are lists
str_num_lst += stuff
#Print out the str_num_list to check if everything's ok
print str_num_lst
#Get an overall sum by looping over the string numbers in the str_num_lst
#Can convert to int inside the loop
overall_sum = 0
for str_num in str_num_lst:
overall_sum += int(str_num)
#Print sum
print 'Overall sum is:'
print overall_sum
EDIT:
You are right, reading in the entire file as one line is a good solution, and it's not difficult to do. Check out this post. Here is what the code could look like.
import re
hand = open('a.txt')
all_lines = hand.read() #Reads in all lines as one long string
all_str_nums_as_one_line = re.findall('[0-9]+',all_lines)
hand.close() #<-- can close the file now since we've read it in
#Go through all the matches to get a total
tot = 0
for str_num in all_str_nums_as_one_line:
tot += int(str_num)
print('Overall sum is:',tot) #editing to add ()
I have code to write files to writer and then use writer to write into csv.
This is all using 'import csv' library.
But the problem is, I have numbers like 123456789.123456789 and when it writes into csv the number got truncate into 123456789.1234. so I want to write the results to txt instead, but don't know how to write rows to txt.
if results.get('rows', []):
for row in results.get('rows'):
for i in range(len(row)):
old, new = row[i], str()
for s in old:
new += s if s in string.printable else ''
row[i]=new
writer.writerow(row)
path = '/Users/sailormoon/Desktop/'
filename = 'sailormoon.csv'
with open(path + filename, 'wt') as f:
writer = csv.writer(f, lineterminator='\n')
Pseudo code I think:
path = '/Users/sailormoon/Desktop/'
filename = 'sailormoon.txt'
with open(path + filename, 'wt') as f:
f.write(row)
what library I can use to write rows into txt for python? Thanks!
Append all your numbers to a list as strings. Then use a for loop:
numbers = ['123456789.123456789', '123453459.128967678']
with open(path + '\\numbers.txt', 'w') as out:
for n in numbers:
out.write(n + '\n')
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')
I have a code here on Python 2.7 that is supposed to tell me the frequency of a letter or word within a single text file.
def frequency_a_in_text(textfile, a):
"""Counts how many "a" letters are in the text file.
"""
try:
f = open(textfile,'r')
lines = f.readlines()
f.close()
except IOError:
return -1
tot = 0
for line in lines:
split = str(line.split())
k = split.count(s)
tot = tot + k
return tot
print frequency_a_in_text("RandomTextFile.txt", "a")
There's a little bit of extra coding in there - the "try" and "except", but that's just telling me that if I can't open the text file, then it'll return a "-1" to me.
Whenever I run it, it seems to just read the first line and tell me how many "a" letters there are.
You are returning out of the function after the first iteration of your loop.
The return statement should be outside of the loop.
for line in lines:
split = str(line.split())
k = split.count(s)
tot = tot + k
return tot