The print of while loop into text file - python

I've done this in python. When I insert a word, it'll repeated with after a digit
For Example if I insert stack, it-ll print:
stack 1
stack 2
stack 3
stack 4
stack 5
stack 6
stack 7
stack 8
stack 9
I want that python to print the name and the digit in a file text. I searches but didn't find anything.
Code:
pwd=raw_input("Enter a word:")
n=0
n=str(n)
print (pwd,n)
while n<9:
out_file=open("tesxt.txt","w")
n+=1
out_file.write(pwd)
out_file.write(n)
out_file.close()
I want that python to write the words that are generated from the loop.
Thx for the help

First in Python 2.7 print is used without the bracket:
>>> print "hello world"
hello world
Then you should open the file outside the while loop.
out_file = open("test.txt", "w")
i = 0
while n < 9:
# do something here
out_file.close()

Your issue is in redefining n. You start with n as an integer (n = 0), then turned it into a string (n = str(n)).
Try this:
pwd = raw_input("Enter a word: ")
n = 0
print("{} {}".format(pwd, n))
with open("test.txt", "w") as out:
while n < 9:
out.write("{} {}\n".format(pwd, n))
n += 1
That should give you the output you expect, because you never redefine n.
If you want to be both python 2 and 3 compliant, add from __future__ import print_statement to the top of the script, which will allow your print() call to work properly.

You have a few errors:
You try to += 1 on a str object. That's a no-no.
You open a file several times without closing it.
Try taking advantage of the context manager that open uses with a while loop inside. Something like this:
pwd = raw_input("Enter a word: ")
with open("tesxt.txt", "w") as fout:
n = 0
while n <= 9: # this will print 0-9. without the =, it will print 0-8
data = "{} {}".format(pwd, n)
print(data)
fout.write("{}\n".format(data))

pwd = raw_input('Enter a word:')
n = 0
print pwd, n
with open('tesxt.txt','w') as out_file:
while n < 9 :
n += 1
out_file.write('{} {}\n'.format(pwd, n))

Related

How to find and extract values from a txt file?

Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form:
X-DSPAM-Confidence: 0.8475
Count these lines, extract the floating point values from each of the lines, and compute the average of those values and produce an output as shown below. Do not use the sum() function or a variable named sum in your solution.*
This is my code:
fname = input("Enter a file name:",)
fh = open(fname)
count = 0
# this variable is to add together all the 0.8745's in every line
num = 0
for ln in fh:
ln = ln.rstrip()
count += 1
if not ln.startswith("X-DSPAM-Confidence: ") : continue
for num in fh:
if ln.find(float(0.8475)) == -1:
num += float(0.8475)
if not ln.find(float(0.8475)) : break
# problem: values aren't adding together and gq variable ends up being zero
gq = int(num)
jp = int(count)
avr = (gq)/(jp)
print ("Average spam confidence:",float(avr))
The problem is when I run the code it says there is an error because the value of num is zero. So I then receive this:
ZeroDivisionError: division by zero
When I change the initial value of num to None a similar problem occurs:
int() argument must be a string or a number, not 'NoneType'
This is also not accepted by the python COURSERA autograder when I put it at the top of the code:
from __future__ import division
The file name for the sample data they have given us is "mbox-short.txt". Here's a link http://www.py4e.com/code3/mbox-short.txt
I edited your code like below. I think your task is to find numbers next to X-DSPAM-Confidence:. And i used your code to identify the X-DSPAM-Confidence: line. Then I splitted the string by ':' then I took the 1st index and I converted to float.
fname = input("Enter a file name:",)
fh = open(fname)
count = 0
# this variable is to add together all the 0.8745's in every line
num = 0
for ln in fh:
ln = ln.rstrip()
if not ln.startswith("X-DSPAM-Confidence:") : continue
count+=1
num += float(ln.split(":")[1])
gq = num
jp = count
avr = (gq)/(jp)
print ("Average spam confidence:",float(avr))
Open files using with, so the file is automatically closed.
See the in-line comments.
Desired lines are in the form X-DSPAM-Confidence: 0.6961, so split them on the space.
'X-DSPAM-Confidence: 0.6961'.split(' ') creates a list with the number is at list index 1.
fname = input("Enter a file name:",)
with open(fname) as fh:
count = 0
num = 0 # collect and add each found value
for ln in fh:
ln = ln.rstrip()
if not ln.startswith("X-DSPAM-Confidence:"): # find this string or continue to next ln
continue
num += float(ln.split(' ')[1]) # split on the space and add the float
count += 1 # increment count for each matching line
avr = num / count # compute average
print(f"Average spam confidence: {avr}") # print value

How to prompt the user to input a file into a function along with a min and max?

I need to make it so rather than this function relying on an parameters from the user when they call the function, it instead gets called, and then prompts the user to enter a FILE name for it to read (ex. they enter "dna.txt"), and then prompts them to enter a mink and a maxk and then it runs through the code of going through this file and finding the most common substring within the given mink and maxk. This is my current code:
def mostCommonSubstring(dna, mink, maxk):
count = 0
check = 0
answer = ""
k = mink
while k <= maxk:
for i in range(len(dna)-k+1):
sub = dna[i:i+k]
count = 0
for i in range(len(dna)-k+1):
if dna[i:i+k] == sub:
count = count + 1
if count >= check:
answer = sub
check = count
k=k+1
print(answer)
print(check)
I am under the impression that is needs to look something like this (but this code doesn't work?):
def mostCommonSubstring():
dnaFile = input("Enter file: ")
dna = open(dnaFile, "r")
mink = input("Enter a min: ")
maxk = input("Enter a max: ")
count = 0
check = 0
answer = ""
k = mink
while k <= maxk:
for i in range(len(dna)-k+1):
sub = dna[i:i+k]
count = 0
for i in range(len(dna)-k+1):
if dna[i:i+k] == sub:
count = count + 1
if count >= check:
answer = sub
check = count
k=k+1
print(answer)
print(check)
(The DNA file is a large file that contains many many a, g, t, and c, sequences. I wanted to be able to have the user input this file along with a min and max and then have the program find the longest common string.)
I know I have a high chance of being wrong here but I'll try to help anyway.
As a beginner I examined your code, and I think you could more use something like this:
with open(dna, 'r') as dnaFile:
# Do whatever you want here...
this will let you to run the file as a string.
IF I am not wrong, your problem was that you indeed opened the file, but you have not actually read it into a string. Thus you tried to access a file as if its contents we're already pressed into a string.
EDIT:
You could also do something like:
dna = open(dnaFile, 'r') # This is from your code.
dnaString = dna.read()
This way you also would read the file's content into a string and continue to run on your code.
Good luck and best regards!

Fastest way to print this in python?

Suppose I have a text file containing this, where the number on the left says how many of the characters of the right should be there:
2 a
1 *
3 $
How would I get this output in the fastest time?
aa*$$$
This is my code, but has N^2 complexity:
f = open('a.txt')
for item in f:
item2=item.split()
num = int(item2[0])
for i in range(num):
line+=item2[1]
print(line)
f.close()
KISS
with open('file.txt') as f:
for line in f:
count, char = line.strip().split(' ')
print char * int(count),
Just print immediately:
for item in open('a.txt'):
num, char = item.strip().split()
print(int(num) * char, end='')
print() # Newline
You can multiply strings to repeat them in Python:
"foo" * 3 gives you foofoofoo.
line = []
with open("a.txt") as f:
for line in f:
n, c = line.rstrip().split(" ")
line.append(c * int(n))
print("".join(line))
You can print directly but the code above lets you get the output you want in a string if you care about that.
Using a list then joining is more efficient than using += on a string because strings are immutable in Python. This means that a new string must be created for each +=. Of course printing immediately avoids this issue.
You can try like this,
f = open('a.txt')
print ''.join(int(item.split()[0]) * item.split()[1] for item in f.readlines())
Your code is actually O(sum(n_i)) where n_i is the number in the row i. You can't do any better and none of the solutions in the other answers do, even if they might be faster than yours.

How to compare sentence character by character in python?

I want to write a code to count number of words in a given sentence by using character comparison and below is the code I have written as I am not allowed to use some fancy utilities like split(), etc. So, could you please guide me where am I making mistakes' I am a novice in python and currently trying to fiigure out how to do charactery by character comparison so as to find out simple counts of words, lines, strings withous using built in utitilites. So, kindly guide me about it.
Input Sentence : I am XYZ
Input_Sentence = raw_input("Enter your sentence: ")
print Input_Sentence
count = 0
i=0
while(Input_Sentence[i] != "\n"):
if(Input_Sentence[i] == ' '):
count=count+1
i+=1
else:
i+=1
print ('Number of Words in a given sentence is :' +str(count))
At first I wouldn't use a while loop in this context. Why not using a for loop?
for char in Input_sentence:
With this you iterate over every letter.
Then you can use the rest of you code and check:
if char == ' ':
# initialize the counter
word_count = 0
last_space_index = 0
# loop through each character in the sentence (assuming Input_Sentence is a string)
for i, x in enumerate(Input_Sentence): # enumerate to get the index of the character
# if a space is found (or newline character for end of sentence)
if x in (' ', '\n'):
word_count += 1 # increment the counter
last_space_index = i # set the index of the last space found
if len(Input_Sentence) > (last_space_index + 1): # check if we are at the end of the sentence (this is in case the word does not end with a newline character or a space)
word_count += 1
# print the total number of words
print 'Number of words:', word_count
The following will avoid errors if there's an space at the beginning or the end of the sentence.
Input_Sentence = raw_input("Enter your sentence: ")
print Input_Sentence
count = 0
sentence_length = len(Input_Sentence)
for i in range(sentence_length):
if Input_Sentence[i] == " ":
if i not in (0, sentence_length - 1):
count += 1
count += 1
print "There are %s words in the sentence \"%s\"." % (count, Input_Sentence)
You may use try-except syntax.
In your code you used while(Input_Sentence[i] != "\n") to find when the sentence comes to an end. If you just print the output at every step before i+ = 1 like this:
...
while(Input_Sentence[i] != "\n"):
...
print i,Input_Sentence[i]
i+=1
else:
print i,Input_Sentence[i],'*'
i+=1
...
you can see for yourself that the output is something like this:
Enter your sentence: Python is good
Python is good
0 P *
1 y *
2 t *
3 h *
4 o *
5 n *
6
7 i *
8 s *
9
10 g *
11 o *
12 o *
13 d *
Traceback (most recent call last):
File "prog8.py", line 19, in <module>
while(Input_Sentence[i] != "\n"):
IndexError: string index out of range
which means that the code that you have written works fine upto the length of the input sentence. After that when i is increased by 1 and it is demanded of the code to check if Input_Sentence[i] == "\n" it gives IndexError. This problem can be overcome by using exception handling tools of Python. Which leaves the option to neglect the block inside try if it is an exception and execute the block within except instead.
Input_Sentence = raw_input("Enter your sentence: ")
print Input_Sentence
count = 0
i=0
try:
while (Input_Sentence[i] != "\n"):
if (Input_Sentence[i] == ' '):
count=count+1
i+=1
else:
i+=1
except:
count = count+1
print ('Number of Words in a given sentence is :' +str(count))

Stdin issues using Python

I recently participated in hackathon for the first time and got stuck on the first problem. I solved the algorithm, but couldn't figure out how to take values from stdin using Python. This is the question:
There are two college students that want to room together in a dorm. There are rooms of various sizes in the dormitory. Some rooms can accomodate two additional students while others cannot.
Input: the first input line will be a number n (1 ≤ n ≤ 100), which is the total number of rooms in the dorm. There will be n lines following this, where each line contains two numbers, p and q (0 ≤ p ≤ q ≤ 100). P is the number students already in the room, while q is the maximum number of students that can live in the room.
Output: print the number of rooms that the two students can live in.
This is my solution. I've tested it using raw_input() and it works perfectly on my interpreter, but when I change it to just input() I get an error message.
def calcRooms(p, q):
availrooms = 0
if q - p >= 2:
availrooms += 1
return availrooms
def main():
totalrooms = 0
input_list = []
n = int(input())
print n
while n > 0:
inputln = input().split() #accepts 2 numbers from each line separated by whitespace.
p = int(inputln[0])
q = int(inputln[1])
totalrooms += calcRooms(p, q)
n -= 1
return totalrooms
print main()
The error message:
SyntaxError: unexpected EOF while parsing
How do I accept data correctly from stdin?
In this particular case, use raw_input to take the entire line as string input.
inputln = raw_input().split()
This takes input line as a string and split() method splits the string with space as delimiter and returns a list inputln
The following code works the way you wanted.
def main():
totalrooms = 0
input_list = []
#n = int(input("Enter the number of rooms: "))
n = input()
while n > 0: # You can use for i in range(n) :
inputln = raw_input().split() #Converts the string into list
p = int(inputln[0]) #Access first element of list and convert to int
q = int(inputln[1]) #Second element
totalrooms += calcRooms(p, q)
n -= 1
return totalrooms
Or, alternatively you may use fileinput.
If input file is not passed as command line argument, stdin will be the default input stream.
import fileinput
for line in fileinput.input() :
#do whatever with line : split() or convert to int etc
Please refer : docs.python.org/library/fileinput.html
Hope this helps, drop comments for clarification if needed.

Categories

Resources