I'm trying to write code that receives two texts and thereafter I am writing code that finds common words among the texts. This is put in a new list. sys.stdin.read() is being used instead of input() because I need to process a long piece of text.
Below is what I wrote thus far. When I run the code it seems to hang because its only asking for input for text1 and never reaches text 2 input request.
What is going on and how can I fix it?
size text 1 = approx. 500.000 chars.
size text 2 = approx. 500.000 chars.
import sys
text1 = sys.stdin.read()
print(text1)
text2 = sys.stdin.read()
print(text2)
# ... snippet ... compare code here ...
I think this will work
text1 = input("Input some text: ")
text2 = input("Input some text: ")
I don't actually know whats going wrong in yours thoough
In the code below you have two different type of input(). At text-1 you add unlimited amount of text, press enter, then type "EOF!' and press enter to go to input for text 2. At text-2 you have the input() bound to the amount of characters (likely to be ASCII or UTF-8 type; accepts all types as written for now). When 500.000 characters is received text-2 is done and you can go to the compare step, which you might have written by now. If that gives problems too post a new question for that.
The print statements are there to show you what the input per text is minus "EOF!" marker for text-1. It can be any type of marker but End Of File seemed to be obvious for now to explain the stop-marker.
import sys
text1 = ''
text2 = ""
nr_lines = 2
while True:
text1 += input()+'\n'
# print(text1[-5:-1])
if text1[-5:-1] == 'EOF!':
break
print(f'\nlen: {len(text1)}, text1 {text1}\n\n'[:-7])
while True:
text2 = input()
if len(text2) >= 500000:
break
print(f'\nlen: {len(text2)}, text2 {text2}\n\n')
The reason why sys.stdin.read() is not working is due to a different purpose and therefore the lack of functionality written into the method. Its one direction communication and not bi-directional.
Related
I'm learning python and one of my labs required me to:
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.
My code ended up being:
char = input()
string = input()
count = 0
for i in string:
if i == char:
count +=1
if count > 1 or count == 0:
print(f"{count} {char}'s")
else:
print(f'{count} {char}')
Whenever I run the code in Thonny or in the Zybooks development tab it works but when I select the submit option I keep getting and EOF error:
Traceback (most recent call last):
File "main.py", line 2, in <module>
string = input()
EOFError: EOF when reading a line
Does anyone know what's causing the error?
I tried using the break command but it didn't help though I think if I used break at the end of my for statement it wouldn't count all the way. Any ideas folks?
Thank you Mr. Roberts the number of inputs was the issue. I had to create a single input and pull what I needed from that single line. My code ended up being:
string = input()
char = string[0]
phrase = string[1:]
count = 0
for i in phrase:
if i == char:
count +=1
All good now.
So I've been trying to make a code that specifically changes the text in a .txt file in a certain folder - and so far it works. But there's one problem: it won't update in real time (inside the code) until I repeat the code (as in, I choose 'Y'), but when I use the script, it changes in real time outside on the file itself.
#Função
def coca():
print("Write your anotation:")
escolha = input()
arquivo1 = open('doc.txt', 'a')
arquivo1.write(escolha)
arquivo1.write(" ")
print("Your anotation:")
f = open("DOC.txt", "r")
print(f.read())
arquivo1.write("\n")
# Main
x = 0
# Loop
while x < 1:
coca()
repetir = input("Do you wish to add anything else to your annotation? Y or N ").lower()
if repetir.upper() == 'N':
print('Goodbye.')
x = x + 1
Basically, say I type an anotation called "This ivory leg is what propels me, harpoons thrust in the sky" on the input, when it follows up printing the text, it shows a blank slate (if the file has nothing written on it) OR the contents of the file before I added a new anotation to it - So if I had on the .txt "Break your backs and crack your oars, men, if you wish to prevail" the "ivory leg" part wouldn't show up, until I started the code again.
I believe it's because the text will only update once the main code is finished, which means the print function only shows what's currently on the doc.txt, since the script hasn't finished yet.
Sorry for my english by the way. It's not my native language!
Close your arquivo1 file after writing.
def coca():
print("Write your anotation:")
escolha = input()
arquivo1 = open('doc.txt', 'a')
arquivo1.write(escolha)
arquivo1.write(" \n")
arquivo1.close()
print("Your anotation:")
f = open("DOC.txt", "r")
print(f.read())
f.close()
Now this would work fine.
I've been writing Python code for only about 4 weeks. I'm writing a little text based game to learn and test everything I know. I can easily make this work with a value entered into the console as an integer, but for whatever reason I can't get my code to work with reading this value from a text file.
Earlier in the program, my code saves a value to a text file, just one value, then later it opens the same text file, over-writes the value with a new value based on a very simple calculation. That calculation is the first value, plus 5. I've spent a bunch of time reading on this site and going through my books and at this point I'm pretty sure I'm just missing something obvious.
The first piece of code that creates the doc and sets the value:
def set_hp(self):
f = open('player_hp.txt', 'w')
self.hitpoints = str(int(self.hitpoints))
f.write(self.hitpoints)
f.close()
This is the trouble section...I have commented the line with the problem.
def camp_fire():
print
print "You stop to build a fire and rest..."
print "Resting will restore your health."
print "You gather wood from the ground around you. You spark\n\
your flint against some tinder. A flame appears.\n\
You sit, and close your eyes in weariness. A peaceful calm takes you\n\
into sleep."
f = open('player_hp.txt', 'r')
orig_hp = f.readlines()
orig_hp = str(orig_hp)
f = open('player_hp.txt', 'w')
new_value = orig_hp + 5 ##this is where my code breaks
new_value = str(int(new_value))
f.write(new_value)
f.close()
print "You have gained 5 hitpoints from resting. Your new HP are {}.".format(new_value)
This is the error I get:
File "C:\Python27\Awaken03.py", line 300, in camp_fire
new_value = orig_hp + 5
TypeError: cannot concatenate 'str' and 'int' objects
I know you can't concatenate a string and an integer, but I keep trying different methods to convert the string to an integer to do the quick math, but I can't seem to get it right.
Error message is clear, you are trying to concatenate string with an integer. You should change the line from:
new_value = orig_hp + 5
To:
new_value = str(int(orig_hp) + 5)
Then you can use above value to write directly into file as a string as:
##new_value = str(int(new_value))## Skip this line
f.write(new_value)
f.readlines() returns a list of lines, in your case something like ['10']. So str(orig_hp) is a textual representation of this list, like '[\'10\']', which you won't be able to interpret as an integer.
You can just use f.read() to read the whole file at once in a string, which will be something like '10', and convert it to a integer:
orig_hp = int(f.read())
I am writing a tool to parse ID3 tags from files an edit them in a GUI fashion. Up until now everything is great. However I am trying to remove the null byte terminators when displaying the info and then adding it back when user saves it to preserver the ID3v1 format. However when doing a check for the null terminator I get nothing.
This is the portion of the code related to the handlig of the tag:
if(bytes.decode(check) == "TAG"):
title = self.__clean(bytes.decode(f.read(30)))
artist = self.__clean(bytes.decode(f.read(30)))
album = self.__clean(bytes.decode(f.read(30)))
year = bytes.decode(f.read(4))
comment = self.__clean(bytes.decode(f.read(30)))
tmp_gen = bytes.decode(f.read(1))
genre = self.__clean(Utils.genreByteToString(tmp_gen))
return TagV1(title, artist, album, year, comment, genre)
return None
The clean method is here:
def __clean(self, string):
counter = 0
for i in range(0, len(string)):
w = string[i]
if(not w.strip()) or b"\00" == w or w == b"00" or w == bytes.decode(b"\00"):
counter+=1
else:
counter = 0
if(counter == 2):
return string[0:i-1]
return string
I've tried every possible combination know of null byte. Either not w or not w.split() I even tried putting it in bytes and then looping thorught that for null byte but still nothing. My counter always stays 0 on the debugger. Also when trying to copy the value from the debugger it appears as this which is an empty space. In the debugger it appears as an empty square. I would appreciate the input.
Using PyChar 2017 1.4
I figured out that the only solution that works is to use
w == str.decode(b"\00") or rstrip("\0")
as denoted by Marteen
Everything else seems to not work. However there are still some places where it doesn't work. For example the comment in the file I am trying doesn't have null bytes until the last one.
Upon further inspection with a hex editor I have found some odd characters. The comment continues on with the \20 character in hex until position 29 where a null character is (for denoting it has a track indicator) the next character is a \01 for the track. Oddly the genre indicator is a 0C which translates to (cannot paste it, it's a box with ceros in it).
EDIT: Using the __clean() method checking for decoded null terminator aswell as w.isspace() seemed to fix the issue in both other cases.
Here is a part of a python script I have:
textString = raw_input('')
text = list(textString)
print textString
try:
for i in range (0, len(text)):
chat_client.sock.send(text[i])
i = i + 1
chat_client.sock.send(0)
except:
Exception
try:
for i in range (0, len(text)):
chat_server.conn.send(text[i])
i = i + 1
chat_server.conn.send(0)
except:
Exception
I then am hoping to put it back together when it is received, using the int delimiter 0. Just for testing purposes, I have got:
byte = self.conn.recv(1024)
if byte:
print byte
else:
break
just to show each byte that has been received individually.
However, when I insert a string, some of it is split into more than one character:
e.g. The quick brown fox jumps over the lazy dog -->
T
h
e
q
u
i
ck
b
r
o
wn
f
ox j
umps ov
er the
lazy dog
I wondered if anyone could figure out why this might be going on.
Thank you in advance.
Also, in case you are wondering why I am trying to split text like this, it is due to a suggestion from this post:
Python P2P socket chat script - only fully working on home network; connects at school but does not work
It is by design on stream socket. From the wikipedia page : a stream socket is a type of internet socket which provides a connection-oriented, sequenced, and unique flow of data without record boundaries. If multiple messages are already present when you read, they may be concatenated.
All what is guaranteed by the specification, if that you get all, and in order.