String Comparison Doesn't Seem to Work for Lines Read from a File

String comparison doesn't seem to work for lines read from a file

Why doesn't "We have a match!" get printed? What did I miss?

If you will notice the output on console you are getting double \n (newlines) one because of print second because in file every line has \n at the end. Your file that looks like:

Line 1
Line 2
Line 3
Line 4

is basically: Line 1\nLine 2\nLine 3\nLine 4 (assuming you don't have any extra white spaces e.g. tabs, blank space).

In for loop: for line in f:, end char of line is \n (and line that you think is 'Line 3' is actually 'Line 3\n') Hence your if condition fails.

To remove that \n just use str.strip([chars]); function. The method strip() returns a copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters).

So, replace if myline == line: by if myline == line.strip() that is what @C.B.'s answer.

Python string comparison not matching a line read from a file

There probably is a line break or other whitespace at the end of the line, so == won't work, unless you trim it first:

if line.strip() == "^^^^^^^^^":

Python string comparison not working for matching lines

The lines that you read from skeleton have new lines at the end, so the exact string comparison are probably not going to work. If you do line = line.strip() as the first line of your loop it will remove whitespace from before and after any text on the line, and might get you closer to what you want.

Can't compare the first line of .txt file with a string

I created a file with the values you are giving:

Michael,70,90,20
John,90,80,60
Molly,60,30,50

And when I try your code, it seems to work fine:

makeList("Michael");
makeList("John");
makeList("Molly");

return

60
77
47

My suspicion is that you have some kind of invisible character at the very beginning of your file, and that is what makes your equality fail. I encountered this kind of issue several time when parsing XML and the parser would complain that my file doesn't start with an XML tag.

Can you try to make a brand new file with these 3 lines and try your program again on this new file?

File's string dont match

with readlines() you get a list with each line as a member of the list, this include the \n (return line code).

Print the whole list to see what I am saying.

with open('/root/file.txt', 'r') as f:
data = f.readlines()
print data

To avoid the \n use this code:

data[10].strip() == 'Password'

If you just need to print something if the word Password is in there, try this code:

if 'Password' in data[10]:
print 'yes'

reading from a file and checking if word in list

You need to strip the line endings from the file, as they are carrying \n or \r\n(Windows carriage return) at the ends. This means you are comparing banana with banana\n, which are not equal, leading to nothing being appended.

You can fix this by using str.strip() beforehand like so:

item = item.strip()
if item in fruit_list:
# rest of code

Or you can just strip from the right with str.rstrip():

item = item.rstrip()
if item in fruit_list:
# rest of code

You could also strip everything while iterating with map():

for item in map(str.strip, r):
# rest of code

But the first two solutions are fine.

C++ if statement comparing strings not working

I tested this code on www.compileonline.com and repeated your findings.

In that environment each string read from the file has a \r character on the end of it.

When I changed the terminating line to if (line == "XXX\r") the code worked as expected.

It seems that your input file's lines are terminated with "\r\n" which is the norm for windows, but unix text files are normally terminated with '\n'.

Update:

Here's a little demo of how one might remove trailing carriage returns and linefeeds (or anything else you want for that matter):

#include <string>
#include <algorithm>
#include <iostream>

void trim_cruft(std::string& buffer)
{
static const char cruft[] = "\n\r";

buffer.erase(buffer.find_last_not_of(cruft) + 1);
}

void test(std::string s)
{
std::cout << "=== testing ===\n";
trim_cruft(s);
std::cout << s << '\n';
}

int main()
{
test(""); // empty string
test("hello world\r"); // should trim the trailing \r
test("hello\nworld\r\n"); // don't trim the first newline
}


Related Topics



Leave a reply



Submit