I have this code:
afile = "name.txt"
f = open(afile,"r")
content = f.readlines()
f.close()
correct = content[1]
answer = raw_input()
if answer == correct:
print 'True'
Let say that, because of the name.txt, content[1] is George and then I run the code and I type George for answer.
Why I won't get True?
Why answer and correct are not the same?
The data you read includes newlines; strip those from the lines first:
if answer == correct.strip():
which removes all whitespace from the start and end of a string. If whitespace at the start or end is important, you can remove just newlines from the end with:
if answer == correct.rstrip('\n'):
Rewritten a bit:
def get_file_line(fname, line_num):
with open(fname) as inf:
try:
for _ in range(line_num + 1):
line = inf.next()
return line.rstrip('\r\n')
except StopIteration:
return None
if answer == get_file_line('name.txt', 1):
print('True')
Related
How can I read only first symbol in each line with out reading all line, using python?
For example, if I have file like:
apple
pear
watermelon
In each iteration I must store only one (the first) letter of line.
Result of program should be ["a","p","w"], I tried to use file.seek(), but how can I move it to the new line?
ti7 answer is great, but if the lines might be too long to save in memory, you might wish to read char-by-char to prevent storing the whole line in memory:
from pathlib import Path
from typing import Iterator
NEWLINE_CHAR = {'\n', '\r'}
def first_chars(file_path: Path) -> Iterator[str]:
with open(file_path) as fh:
new_line = True
while c := fh.read(1):
if c in NEWLINE_CHAR:
new_line = True
elif new_line:
yield c
new_line = False
Test:
path = Path('/some/path/a.py')
easy_first_chars = [l[0] for l in path.read_text().splitlines() if l]
smart_first_chars = list(first_chars(path))
assert smart_first_chars == easy_first_chars
file-like objects are iterable, so you can directly use them like this
collection = []
with open("input.txt") as fh:
for line in fh: # iterate by-lines over file-like
try:
collection.append(line[0]) # get the first char in the line
except IndexError: # line has no chars
pass # consider other handling
# work with collection
You may also consider enumerate() if you cared about which line a particular value was on, or yielding line[0] to form a generator (which may allow a more efficient process if it can halt before reading the entire file)
def my_generator():
with open("input.txt") as fh:
for lineno, line in enumerate(fh, 1): # lines are commonly 1-indexed
try:
yield lineno, line[0] # first char in the line
except IndexError: # line has no chars
pass # consider other handling
for lineno, first_letter in my_generator():
# work with lineno and first_letter here and break when done
You can read one letter with file.read(1)
file = open(filepath, "r")
letters = []
# Initilalized to '\n' to sotre first letter
previous = '\n'
while True:
# Read only one letter
letter = file.read(1)
if letter == '':
break
elif previous == '\n':
# Store next letter after a next line '\n'
letters.append(letter)
previous = letter
So I have to write a code that first reads in the name of an input file, followed by two strings representing the lower and upper bounds of a search range. The file should be read using the file.readlines() method. The input file contains a list of alphabetical, ten-letter strings, each on a separate line. The program should output all strings from the list that are within that range (inclusive of the bounds).
The text file (input1.txt) contains:
aspiration
classified
federation
graduation
millennium
philosophy
quadratics
transcript
wilderness
zoologists
so for example, if i input:
input1.txt
ammoniated
millennium
the output should be:
aspiration
classified
federation
graduation
millennium
So far, I tried:
# prompt user to enter input1.txt as filepath
filepath = input()
start = input()
end = input()
apending = False
out = ""
with open(filepath) as fp:
line = fp.readline()
while line:
txt = line.strip()
if(txt == end):
apending = False
# how do I make it terminate after end is printed??
if(apending):
out+=txt + '\n'
if(txt == start):
apending = True
line = fp.readline()
print(out)
However, it does not seem to output anything. Any help debugging or fixing my code is greatly appreciated~
here is the code:
# prompt user to enter input1.txt as filepath
filepath = input()
start = input()
end = input()
# apending = False
# out = ""
with open(filepath) as fp:
while True:
line = fp.readline()
if not line:
break
txt = line.strip()
if txt >= start and txt <= end:
print(txt)
if txt > end:
break
None of the strings in the input is equal to ammoniated, therefore txt == start is never true, therefore apending is never set to True, therefore out += txt + '\n' is never executed, therefore print(out) prints an empty string at the end.
You should use < or > to compare the strings from the input with start and end. They are not supposed to be exact matches.
This can be done by comparing strings using the <= and >= operators, and then filtering the words that match the lower and upper bounds criteria.
As said in this article, "Python string comparison is performed using the characters in both strings. The characters in both strings are compared one by one. When different characters are found then their Unicode value is compared. The character with lower Unicode value is considered to be smaller."
f = open('input.txt')
words = f.readlines()
lower_bound = input('Set the lower bound for query')
upper_bound = input('Set the upper bound for query')
result = [word.strip() for word in words if lower_bound <= word.strip() <= upper_bound]
print(result)
f.close()
output for print(result), with the input you provided:
['aspiration', 'classified', 'federation', 'graduation', 'millennium']
You can exit out of a for loop by using the keyword break
for example
for x in range(50):
if x > 20:
break;
else:
print(x);
I'm trying to write a function that reads through a text file until it finds a word (say "hello"), then print the next x lines of string starting with string 1 (say "start_description") until string 2 (say "end_description").
hello
start_description 123456 end_description
The function should look like description("hello") and the following output should look like
123456
It's a bit hard to explain. I know how to find the certain word in the text file but I don't know how to print, as said, the next few lines between the two strings (start_description and end_description).
EDIT1:
I found some code which allows to print the next 8, 9, ... lines. But because the text in between the two strings is of variable length, that does not work...
EDIT2:
Basically it's the same question as in this post: Python: Print next x lines from text file when hitting string, but the range(8) does not work for me (see EDIT1).
The input file could look like:
HELLO
salut
A: 123456.
BYE
au revoir
A: 789123.
The code should then look like:
import re
def description(word):
doc = open("filename.txt",'r')
word = word.upper()
for line in doc:
if re.match(word,line):
#here it should start printing all the text between start_description and end_description, for example 123456
return output
print description("hello")
123456
print description("bye")
789123
Here's a way using split:
start_desc = 'hello'
end_desc = 'bye'
str = 'hello 12345\nabcd asdf\nqwer qwer erty\n bye'
print str.split('hello')[1].split('bye')[0]
The first split will result in:
('', ' 12345\nabcd asdf\nqwer qwer erty\n bye')
So feed the second element to the second split and it will result in:
('12345\nabcd asdf\nqwer qwer erty\n ', '')
Use the first element.
You can then use strip() to remove the surrounding spaces if you wish.
def description(infilepath, startblock, endblock, word, startdesc, enddesc):
with open(infilepath) as infile:
inblock = False
name = None
found = False
answer = []
for line in infile:
if found and not inblock: return answer
if line.strip() != startblock and not inblock: continue
if line.strip() == startblock: inblock = True
elif line.strip() == endblock: inblock = False
if not line.startswith(startdesc):
name = line.strip()
continue
if name is not None and name != word: continue
if not line.startswith(startdesc): continue
answer.append(line.strip().lstrip(startdesc).rstrip(enddesc))
I have a small script which goes like this:
(Notice the comparison)
def readVariations(path_to_source_config):
varsTuple = []
source_file = open(path_to_source_config, "r")
for line in source_file:
line_no_spaces = line.replace(" ","")
if line_no_spaces[0] == '[':
current_line_ = line_no_spaces.replace("[", "")
current_line = current_line_.replace("]", "")
section = "ExecutionOptimizer"
if current_line == section:
print current_line
#_tuple = section_name, option_name, range_start, range_end, step
#varsTuple.append(_tuple)
return varsTuple
What it does is reads a config file (.cfg) and needs to check if it finds a particular string.
The following line comes up in the config file:
[ExecutionOptimizer]
For some reason the comparison is failing when the same string is encountered in the file.
Can you please tell me why.
I suspect line ends with a newline character, and it remains there throughout all of your replace operations. Then your comparison fails because "ExecutionOptimizer\n" doesn't equal "ExecutionOptimizer". You can discard the newline using strip:
line_no_spaces = line.strip().replace(" ","")
Use "is" key word.
"==" is for equality testing
From a Python interpreter:
> a = 'tea'
> b = ''.join(['t', 'e', 'a'])
> a == b
True
> a is b
False
I was wondering how I can print all the text between a \begin statement and a \end statement.
This is my code now.
Also, how can I keep from printing certain words located between these 2 statements?
content=open("file", "r")
print content
content.read()
while len(content.split(start,1)) > 1:
start=("\begin")
end=("\end")
s=content
print find_between( s, "\begin", "\end" )
def find_between( s, first, last ):
try:
start = s.index( first ) + len( first )
end = s.index( last, start )
return s[start:end]
except ValueError:
return ""
print find_between( s, "\begin", "\end" )
This example presumes you don't mind loosing the data on the \begin and \end lines. It will print all occurrences of data between \begin and \end.
f = open("file", "r")
content = f.readlines()
f.close()
start = "\\begin"
end = "\\end"
print "Start ==", start, "End ==", end
printlines = False
for line in content:
if start in line:
printlines = True
continue
if end in line:
printlines = False
continue
if printlines == True:
print line
Input file -
test
\begin do re me fa
so la te do.
do te la so \end fa me re do
Output -
Start == \begin End == \end
so la te do.
Assuming there is only one "\begin" to "\end" block in the file:
f = open('file', 'r')
between = ''
in_statement = False
for line in f:
if '\begin' in line:
in_statement = True
if in_statement:
between += line
if '\end' in line:
in_statement = False
break
print between
f.close()
regex is good for this sort of thing.
In [152]: import re
In [153]: s = 'this is some \\begin string that i need to check \end some more\\begin and another \end stuff after'
In [167]: re.findall(r'\\begin(.*?)\\end', s)
[' string that i need to check ',
' and another ']
The regex:
use raw string because \ means something to the regex parser.
\begin and \end are raw character strings to match. You have to do the backslash twice because backslash means 'special' for the regex, so you need \ to actually match a backslash.
.*? = dot matches anything, * means match 0 or more repetitions. The ? turns off greedy behaviour - otherwise, it will match everything between the FIRST begin and the LAST end, instead of all in between matches.
and findall then gives you a list of all matches.