Print character amount to the file Python - python

My code pretty much asks user input and a pre made text which it prints to the .txt file. I want to add the character calculator to print into the file, not as a print. Is it possible?
input = input(str("Type something: "))
file = open("harjoitus.txt", "w")
file.write(str("This code is here automatically.\n"))
file.write(str(input))
file.write(str("\n",))
file.close()
with open("harjoitus.txt", 'r') as file:
text = file.read().strip().split()
len_chars = sum(len(word) for word in text)
print(len_chars)
This pretty much prints the amount of characters as print not to the text file how I want it. Is it possible to edit this somehow that it prints the character amount straight to the file and not as a print?

First before you go into this appending, take a look at how many places you are calling str() its unnecessary most of these values are already stings and ready to be written. Also avoid variable names like input that have preassigned purposes in python. But to add this count to the end, collections.Counter is an option, you should open the file as a append. Then you can add this number to the end of your file.
from collections import Counter
user = input("Type something: ")
with open('harjoitus.txt', 'w') as f:
f.write("This code is here automatically.\n")
f.write(user)
f.write("\n")
with open('harjoitus.txt', 'r') as f:
content = f.read()
c = Counter(content)
tot = sum(c.values())
with open('harjoitus.txt', 'a') as f:
f.write(str(tot))
chrx#chrx:~/python/stackoverflow/10.11$ python3.7 stack.py
Type something: vash
chrx#chrx:~/python/stackoverflow/10.11$ cat harjoitus.txt
This code is here automatically.
vash
38

Related

Dividing a .txt file in multiple parts in Python

I'm a begginer in Python, and I have a question about file reading :
I need to process info in a file to write it in another one. I know how to do that, but it's reaaally ressource-consuming for my computer, as the file is really big, but I know how it's formatted !
The file follows that format :
4 13
9 3 4 7
3 3 3 3
3 5 2 1
I won't explain what it is for, as it would take ages and would not be very useful, but the file is essentialy made of four lines like these, again and again. For now, I use this to read the file and convert it in a very long chain :
inputfile = open("input.txt", "r")
output = open("output.txt", "w")
Chain = inputfile.read()
Chain = Chain.split("\n")
Chained = ' '.join(Chain)
Chain = Chained.split(" ")
Chain = list(map(int, Chain))
Afterwards, I just treat it with "task IDs", but I feel like it's really not efficient.
So do you know how I could divide the chain into multiple ones knowing how they are formatted?
Thanks for reading !
How about:
res = []
with open('file', 'r') as f:
for line in f:
for num in line.split(' '):
res.append(int(num))
Instead of reading the whole file into memory, you go line by line.
Does this help?
If you need to go 4 lines at a time, just add an internal loop.
Regarding output, I'm assuming you want to do some computation on the input, so I wouldn't necessarily do this in the same loop. Either process the input once reading is done, or instead of using a list, use a queue and have another thread read from the queue while this thread is writing to it.
Perhaps the utility of a list comprehension will help a bit as well (I doubt this will make an impact):
res = []
with open('file', 'r') as f:
for line in f:
res.append( int(num) for num in line.split() )
hmm there's some method to write to a file without reading it i believe
Add text to end of line without loading file
https://docs.python.org/2.7/library/functions.html#print
from __future__ import print_function
# if you are using python2.7
i = open("input","r")
f = open("output.txt","w")
a = "awesome"
for line in i:
#iterate lines in file input
line.strip()
#this will remove the \n in the end of the string
print(line,end=" ",file=f)
#this will write to file output with space at the end of it
this might help, i'm a newbie too, but with better google fu XD
Maybe do it line by line. This way it consumes less memory.
inputfile = open("input.txt", "r")
output = open("output.txt", "a")
while True:
line = inputfile.readline()
numbers = words.split(" ")
integers = list(map(int, numbers))
if not line:
break
There is probably a newline character \n in the words. You should also replace that with an empty string.
If you don't wanna to consume memory (you can run of it if file is very large), you need to read lien by line.
with open('input.txt', 'w') as inputfile, open('"output.txt', 'w') as output:
for line in inputfile:
chain = line.split(" ")
#do some calculations or what ever you need
#and write those numbers to new file
numbers = list(map(int, chain))
for number in numbers
output.write("%d " % number)

Does anyone know why this writing to .txt file doesn't work?

When I run this code and input values for crop and quantity it doesn't write them to crop database.txt like it should, but just creates crop database.txt as a new blank text file. Does anyone know why this is? Is it because of the way I am closing the file?
crop = input("Which crop? ")
quantity = input("How many? ")
with open ('cropdatabase.txt', 'a+') as file:
lines = file.readlines()
file.close ()
with open ('cropdatabase.txt', 'a+') as file:
for row in lines:
if crop in row:
row = str(a)
split_2 = a.split (',')
split_2.append (quantity)
else:
file.write ('\n')
file.write (crop + ' ')
file.write (quantity + ' ')
file.close ()
If you start with an empty file (or non-existing) the list lines will be empty. So your code for writing anything never runs.
Here is your code with comments:
crop = input("Which crop? ")
quantity = input("How many? ")
If you are using python 2, this is a bad idea. use raw_input
with open ('cropdatabase.txt', 'a+') as file:
You are opening this file to append, but are just reading it
lines = file.readlines()
file.close ()
You don't need to close since you used 'with open'
with open ('cropdatabase.txt', 'a+') as file:
Once again you open to append. You should rewrite the file with 'w'
for row in lines:
if crop in row:
row = str(a)
split_2 = a.split (',')
split_2.append (quantity)
This makes no sense to me. What is a?
After mucking with split2 (again, why) you never write anything
else:
file.write ('\n')
file.write (crop + ' ')
file.write (quantity + ' ')
Now you write crop and quantity to lines that don't have a match?
file.close ()
again, unnecessary close
here is my code
crop = raw_input("Which crop? ")
quantity = raw_input("How many? ")
using raw_input, both will be strings
with open ('cropdatabase.txt', 'r') as file:
lines = file.readlines()
Read the complete file into lines, then close it (automatically)
with open ('cropdatabase.txt', 'w') as file:
for row in lines:
if crop in row:
row = row.strip(), ", ", quantity
file.write(row)
You don't specify, so I am writing the row that matches crop and adding the quantity after a comma
else:
file.write(row)
Try this and come back with what you see. First save your input file to another name so you can compare the two
Hahaha small bug.
use open(filename, 'r') for reading a file
use open(filename, 'a') for appending some data to a exsisting file
use open(filename, 'w') for creating/re-writing a file.
when you open a file with appending 'a', state the file pointer is placed just before EOF(end of file) pointer. So you wont be reading any data.
I would prefer nt using 'a+', it looks great but using a specific format gives more clarity for reading code.
try this
with open ('cropdatabase.txt', 'r') as file:
lines = file.readlines()
Hope this helps.
First you need to use raw_input
crop = raw_input("Which crop? ")
quantity = raw_input("How many? ")
Then you do not need f.close() because you have with open.

Displaying integers from file on one line

I have the very simple task of creating a text file with 8 random integers from 1-100, reading the file, displaying the numbers on the same line, calculating the even integers and the odd integers, and then displaying them.
The problem I am having is getting the string to display on the same line. I have browsed multiple articles about similar problems to no avail. I have attempted to use .join, however, it seems to break the code when I include it.
# Imports random and time
import random
import time
# Defines the main function
def main():
# Opens file "mynumbers" and creates it if not existent
myfile = open('mynumbers.txt', 'w')
# Statement to write intergers to text file in the correct format
for count in range(8):
number = random.randint(1, 100)
myfile.write(str(number) + '\n')
# Defines read function
def read():
# Opens the "mynumbers" file created in the main function
myfile= open('mynumbers.txt', 'r')
# Sets the content variable to the content of the file that was opened
content = myfile.read()
# Prints the content variable and strips the \n from the string
stripit = content.rstrip('\n')
print(stripit)
# Calls for the functions, prints created, and sleep calls
main()
print('File Created!')
time.sleep(1)
read()
time.sleep(5)
Any help that can be provided would be greatly appreciated.
Your read function is reading the whole file contents into a single string. Your rstrip call on that string removes the last newline from it, but not any of the internal newlines. You can't effectively use str.join, since you only have the one string.
I think there are two reasonable solutions. The first is to stay with just a single string, but replace all the internal newlines with spaces:
def read():
myfile = open('mynumbers.txt', 'r')
content = myfile.read()
stripit = content.rstrip('\n')
nonewlines = stripit.replace('\n', ' ')
print(nonewlines)
The other approach is to split the single string up into a list of separate strings, one for each number. This is more useful if we need to do different things with them later. Of course, all we're going to do is use join to combine them back together:
def read():
myfile = open('mynumbers.txt', 'r')
content = myfile.read()
content_list = content.split() # by default, splits on any kind of whitespace
rejoined_content = " ".join(content_list)
print(rejoined_content)
Don't add a newline char when you write the file. Just use a space instead (or comma, whatever)
import random
import time
#Defines the main function
def main():
#Opens file "mynumbers" and creates it if not existent
myfile = open('mynumbers.txt', 'w')
#Statement to write intergers to text file in the correct format
for count in range(8):
number = random.randint(1,100)
myfile.write(str(number) +' ')
#Defines read function
def read():
#Opens the "mynumbers" file created in the main function
myfile= open('mynumbers.txt', 'r')
#Sets the content variable to the content of the file that was opened
content=myfile.read()
#Prints the content variable and strips the \n from the string
print(content)
#Calls for the functions, prints created, and sleep calls
main()
print('File Created!')
time.sleep(1)
read()
time.sleep(5)
the code looks great but do this instead on your read() function.
def read():
my_numbers = []
with open('mynumbers.txt', 'r') as infile:
for line in infile:
line = line.strip()
my_numbers.append(line)
print (' '.join(line))
I would do it like this, especially because you mentioned the even and odd part that you'll need to do next. At the end of the first loop, you'll have a list of ints (rather than strs) that you can work with and determine whether they are even or odd.
def read():
my_nums = []
with open('mynumbers.txt', 'r') as f:
for line in f:
num_on_line = int(line.strip())
my_nums += [num_on_line]
print num_on_line, #don't forget that comma
for num in my_nums:
#display the even and odds
You could print the numbers in a single line in this way
with open('mynumbers.txt', 'r') as numbers_file:
for line in numbers_file:
print(line.strip(), end=" ")
The line.strip() is for eliminate the \n character.

Python 3; Writing to text files, unintentional newline (beginner)

The following function is a part of my future program, a library program. This particular function is supposed to fetch books from a text file, and if the user desires, "loan" them and thereby adding a string "(LOANED)" after the author of the book. The books are sorted by title first, followed by a comma and a space (, ) and then the author of the book. What I want to do is to just, simply, add a "(LOANED)" string after the author of the book in the text file. However, when I try this the (LOANED) string just ends up on a different line (one line below) from where I want it to be, and it's driving me nuts.
def lend_book(title):
f = open("booksbytitle.txt", "r+")
d={}
#splits the registry up into a dictionary with the book title as key and the book author as value to that key
for i in f:
x=i.split(",")
a=x[0]
b=x[1]
d[a]=b[0:(len(b))]
#checks if the title is in the dictionary, else skips down and quits the function
if title in d:
print("\n", title, "is available in the library and is written by"+d[title])
saved_author = d[title][1:]
while True:
alternative=input("Do you want to lend this book? [y/n] ")
if alternative.lower() == "y":
print("The book is now loaned ")
break
elif alternative.lower() == "n":
print("Okay, going back to main menu.. ")
break
else:
print("That is not a valid choice. Type 'y' or 'n'")
f.close()
loaned="(LOANED)"
f=open("booksbytitle.txt", "r+")
z=f.readlines()
f.seek(0)
for i in z:
if title not in i:
f.write(i)
f.write("\n" + title + ", " + saved_author + loaned)
f.truncate()
f.close()
#clears the program of unintented blank lines
fh = open("booksbytitle.txt", "r")
lines = fh.readlines()
fh.close()
keep = []
for line in lines:
if not line.isspace():
keep.append(line)
fh = open("booksbytitle.txt", "w")
fh.write("".join(keep))
fh.close()
else:
print("There wasnt a book by that name found in the registry")
It's hard to tell with the screwed-up formatting and the meaningless one-letter variable names, but I suspect the problem is this:
When you iterate the lines of a file, like for i in f:, each one ends with a newline character ('\n').
When you split(",") each one, the last split-off string still contains that newline.
So ultimately, when you try to stick that string in the middle of a string, it's got a newline at the end, which means you end up with a newline in the middle of the string.
To fix this, use rstrip on each line as you read them in:
for i in f:
x = i.rstrip().split(",")
This may mean that you're now missing newlines in your output to the file. You were expecting to get them for free, but now you don't. So you may have to do something like this:
f.write("\n" + title + ", " + saved_author + loaned + "\n")
However, maybe not. I notice that for some reason you're putting a "\n" at the start of every line, so this may just mean you end up with extra blank lines between each line (along with the extra blank line at the start of your file, which is inherent in using "\n" +).
You could use rstrip() on the strings to remove the right spaces (newlines),
and then join over "\n" instead of the empty string.
PS: You can write a bit of this code much simpler, by the way. For instance, you can just get the lines in the file all at once, filter out the empty ones and rstrip all at the same time, like this:
with open(filename, "r") as handler:
keep = [line.rstrip() for line in handler if line]
(The 'with' takes care of automatically closing the file after the indented block, then there's a list comprehension, and the open file object "handler" gives you the lines when iterating over it.)

Editing a text file, then displaying it

In my code, I want to insert words into a text file from a user. So I have these words in the text file that must be replaced by the user input, here are the strings must be replaced in the file , adjective,plural_noun,noun.
file1 = open('Sample.txt', 'w')
*adjective*,*plural_noun*,*noun*,*verb*,*male_first_name* = [
line.strip() for line in open('Sample.txt')]
for t in *adjective* :
print(input("enter an adjective: ", file=file1))
print(input("enter an plural noun: ", file=file1))
print(input("enter an verb: ", file=file1))
file1.close()
A little something to get you started...
file1 = open('Sample.txt', 'r')
text = file1.read()
while (text.find('*replace*') != -1):
inp = raw_input("enter some text to replace: ");
text = text.replace('*replace*', inp, 1)
print(text)
If Sample.txt contains This is some text to *replace* and the user input is xyz, this code prints:
This is some text to xyz
Let's step through it bit by bit:
file1 = open('Sample.txt', 'r') opens the file for reading ('r' means "for reading").
text = file1.read() reads the content of the file and puts it in the variable text.
while (text.find('*replace*') != -1): looks for occurrences of the string *replace* and continues with the indented commands as long as it finds one.
inp = raw_input("enter some text to replace: "), which only runs if there is a remaining occurrence of *replace*, gets user input and puts it in the variable inp.
text = text.replace('*replace*', inp, 1), which also only runs if there is a remaining occurrence of *replace*, replaces the next occurrence of *replace* with the user input, overwriting the old text.
print(text), which runs once all occurrences of *replace* have been replaced with user input, prints out the new text.
This is not how you would write an efficient programme with lots of different *string* strings, but hopefully it will lead you in the right direction and walking before running is often a good idea.
There is excellent online Python documentation and you can also use the pydoc tool -- e.g. pydoc str.replace from the command line.

Categories