I have this code that should generate all possible combinations of digits and store them in a text file called Passwords4.txt. The issue here is that when I go to the text file it just shows 9999 instead of showing the numbers from 0000 to 9999.
import itertools
lst = itertools.product('0123456789', repeat=4) #Last part is equal to the password lenght
for i in lst:
print ''.join(i)
f = open('Passwords4.txt', 'w')
f.write(str(''.join(i)) +'\n')
f.close()
Can someone explain what should I do?
Your f.write is not inside the loop, so it only happens once.
You probably want the open() before the loop, and your f.write in the loop (indented, same as print).
This is the more Pythonic way of doing :
import itertools
lst = itertools.product('0123456789', repeat=4) #Last part is equal to the password lenght
with open('Passwords4.txt', 'w') as f:
for i in lst:
print ''.join(i)
f.write(str(''.join(i)) +'\n')
Python takes care of everything here ...
Re:
for i in lst:
print ''.join(i)
f = open('Passwords4.txt', 'w')
f.write(str(''.join(i)) +'\n')
By the time you open the file and write to it after the loop is finished), i has already been set to just the last result of the loop and that's why you're only getting 9999.
A fix is to do the writes within the loop, with something like:
import itertools
lst = itertools.product('0123456789', repeat=4)
f = open('Passwords4.txt', 'w')
for i in lst:
f.write(''. join(i) + '\n')
f.close()
Related
I have a block of text and I'd like to add a new line character at the end of any line that is fewer than 50 characters.
This is where I'm at
text = open('file.txt','r+')
line_length = []
lines = list(enumerate(text))
for i in lines:
line_length.append(len(i))
print lines
print line_length
I just end up with a large list of the value 2 over and over. I know that the length of each line is not 2.
Edit: Here's the solution I went with
text = open('text.txt','r+')
new = open('new.txt','r+')
new.truncate(0)
l=[]
for i in text.readlines():
if len(i) < 50:
l.append(i+'\n')
else:
l.append(i)
new.write(' '.join(l))
text.close()
new.close()
Well like:
text = open('file.txt','r+')
l=[]
for i in text.readlines():
if len(i)<50:
l.append(i)
else:
l.append(i.rstrip())
No need for enumerate.
Or one-liner ( i recommend this ):
l=[i if len(i)<50 else i.rstrip() for i in text.readlines()]
So your code doesn't work because really of enumerate.
Both cases:
print(l)
Is desired output.
lines is a list of pairs (each with a length of two). You need to check the length of the sublist, not the pair that it's in:
for i, seq in lines:
line_length.append(len(seq))
Although, as you can see, you don't use i, so there's no point in using enumerate.
Assuming you are trying to write to a new file, you will want something like this:
with open("file.txt", "r+") as input_file, open("output.txt", "w") as output_file:
for line in input_file:
if len(line) < 50:
line += '\n'
output_file.write(line)
The lines in your existing file will often have a newline character at the end of them already, so the result will be two newline characters for lines of length under 50. Use rstrip if you need to avoid this.
I would like to create a different number of files with Python 2.7.10. The number of them is determined by the i index ( range(X) where X is already been defined) and the name should be like :file1,file2,file3,file4...filei. Here what I was writing:
list3 = range(X)
for i in list3:
# (here I want to add the creation process)
Thanks to everyone!!
list3 = range(x)
for i in list3:
with open("File%i" % i, "w") as myfile:
#do something
You can just open files and not write anything to them to create them:
for i in range(x):
f = open("file{}".format(i), 'w')
f.close()
The question I guess is a duplicate one. Anyway for instance you can do something like that:
for x in range(3):
with open('File' + str(x) +'.txt','w') as f:
stringa = str(x) + ' Number of file written inside'
f.write(stringa)
Hope this solve your query. Have a nice day and ciao!
I'm using random.randint to generate a random number, and then assigning that number to a variable. Then I want to print the line with the number I assigned to the variable, but I keep getting the error:
list index out of range
Here's what I tried:
f = open(filename. txt)
lines = f.readlines()
rand_line = random. randint(1,10)
print lines[rand_line]
You want to use random.choice
import random
with open(filename) as f:
lines = f.readlines()
print(random.choice(lines))
To get a random line without loading the whole file in memory you can use Reservoir sampling (with sample size of 1):
from random import randrange
def get_random_line(afile, default=None):
"""Return a random line from the file (or default)."""
line = default
for i, aline in enumerate(afile, start=1):
if randrange(i) == 0: # random int [0..i)
line = aline
return line
with open('filename.txt') as f:
print(get_random_line(f))
This algorithm runs in O(n) time using O(1) additional space.
This code is correct, assuming that you meant to pass a string to open function, and that you have no space after the dot...
However, be careful to the indexing in Python, namely it starts at 0 and not 1, and then ends at len(your_list)-1.
Using random.choice is better, but if you want to follow your idea it would rather be:
import random
with open('name.txt') as f:
lines = f.readlines()
random_int = random.randint(0,len(lines)-1)
print lines[random_int]
Since randint includes both boundary, you must look until len(lines)-1.
f = open(filename. txt)
lines = f.readlines()
rand_line = random.randint(0, (len(lines) - 1)) # https://docs.python.org/2/library/random.html#random.randint
print lines[rand_line]
You can edit your code to achieve this without an error.
f = open(filename. txt)
lines = f.readlines()
rand_line = random. randint(0,len(lines)-1) # this should make it work
print lines[rand_line]
This way the index is not out of range.
My code to give all k long substrings of a string (that is string[0:k],string[1:k+1], etc works OK on small test strings, but on long (100 and more) one string in output is missing. What could be a problem and how to fix it? Here is a code (sure, Python):
def possible_kmers (a, b):
kmers = []
i=0
while i<len(a)-b+1:
kmer = a[i:i+b]
kmers.append(kmer)
i=i+1
file = open("result.txt", "w")
kmers.sort()
for item in kmers:
file.write(item+'\n')
file.close()
I'm not sure why your code would be missing a string on large inputs, but this code is cleaner, more encapsulated, and it works:
def possible_kmers(s, sublen):
for i in range(len(s) - sublen + 1):
yield s[i:i+sublen]
kmers = sorted(possible_kmers(SOME_S, SOME_SUBLEN))
with open('result.txt', 'w') as outfile:
for kmer in kmers:
file.write('{}\n'.format(kmer)
I have around around 20000 text files, numbered 5.txt,10.txt and so on..
I am storing the filepaths of these files in a list "list2" that i have created.
I also have a text file "temp.txt" with a list of 500 words
vs
mln
money
and so on..
I am storing these words in another list "list" that i have created.
Now i create a nested dictionary d2[file][word]=frequency count of "word" in "file"
Now,
I need to iterate through these words for each text file as,
i am trying to get the following output :
filename.txt- sum(d[filename][word]*log(prob))
Here, filename.txt is of the form 5.txt,10.txt and so on...
"prob",which is a value that i have already obtained
I basically need to find the sum of the inner keys'(words) values, (which is the frequency of the word) for every outer key(file).
Say:
d['5.txt']['the']=6
here "the" is my word and "5.txt" is the file.Now 6 is the number of times "the" occurs in "5.txt".
Similarly:
d['5.txt']['as']=2.
I need to find the sum of the dictionary values.
So,here for 5.txt: i need my answer to be :
6*log(prob('the'))+2*log(prob('as'))+...`(for all the words in list)
I need this to be done for all the files.
My problem lies in the part where I am supposed to iterate through the nested dictionary
import collections, sys, os, re
sys.stdout=open('4.txt','w')
from collections import Counter
from glob import glob
folderpath='d:/individual-articles'
folderpaths='d:/individual-articles/'
counter=Counter()
filepaths = glob(os.path.join(folderpath,'*.txt'))
#test contains: d:/individual-articles/5.txt,d:/individual,articles/10.txt,d:/individual-articles/15.txt and so on...
with open('test.txt', 'r') as fi:
list2= [line.strip() for line in fi]
#temp contains the list of words
with open('temp.txt', 'r') as fi:
list= [line.strip() for line in fi]
#the dictionary that contains d2[file][word]
d2 =defaultdict(dict)
for fil in list2:
with open(fil) as f:
path, name = os.path.split(fil)
words_c = Counter([word for line in f for word in line.split()])
for word in list:
d2[name][word] = words_c[word]
#this portion is also for the generation of dictionary "prob",that is generated from file 2.txt can be overlooked!
with open('2.txt', 'r+') as istream:
for line in istream.readlines():
try:
k,r = line.strip().split(':')
answer_ca[k.strip()].append(r.strip())
except ValueError:
print('Ignoring: malformed line: "{}"'.format(line))
#my problem lies here
items = d2.items()
small_d2 = dict(next(items) for _ in range(10))
for fil in list2:
total=0
for k,v in small_d2[fil].items():
total=total+(v*answer_ca[k])
print("Total of {} is {}".format(fil,total))
for fil in list2: #list2 contains the filenames
total = 0
for k,v in d[fil].iteritems():
total += v*log(prob[k]) #where prob is a dict
print "Total of {} is {}".format(fil,total)
with open(f) as fil assigns fil to whatever the contents of f are. When you later access the entries in your dictionary as
total=sum(math.log(prob)*d2[fil][word].values())
I believe you mean
total = sum(math.log(prob)*d2[f][word])
though, this doesn't seem to quite match up with the order you were expecting, so I would instead suggest something more like this:
word_list = [#list of words]
file_list = [#list of files]
dictionary = {#your dictionary}
summation = lambda file_name,prob: sum([(math.log(prob)*dictionary[word][file_name]) for word in word_list])
return_value = []
for file_name in file_list:
prob = #something
return_value.append(summation(file_name))
The summation line there is defining an anonymous function within python. These are called lambda functions. Essentially, what that line in particular means is:
summation = lambda file_name,prob:
is almost the same as:
def summation(file_name, prob):
and then
sum([(math.log(prob)*dictionary[word][file_name]) for word in word_list])
is almost the same as:
result = []
for word in word_list:
result.append(math.log(prob)*dictionary[word][file_name]
return sum(result)
so in total you have:
summation = lambda file_name,prob: sum([(math.log(prob)*dictionary[word][file_name]) for word in word_list])
instead of:
def summation(file_name, prob):
result = []
for word in word_list:
result.append(math.log(prob)*dictionary[word][file_name])
return sum(result)
though the lambda function with the list comprehension is much faster than the for loop implementation. There are very few cases in python where one should use a for loop instead of a list comprehension, but they certainly exist.