I'm writing a program in python
and I want to compare two strings that exist in a text file and are separated by a new line character. How can I read the file in and set each string to a different variable. ie string1
and string2
?
Right now I'm using:
file = open("text.txt").read();
but this gives me extra content and not just the strings. I'm not sure what it is returning but this text file just contains two strings. I tried using other methods such as ..read().splitlines()
but this did not yield the result I'm looking for. I'm new to python so any help would be appreciated!
This only reads the first 2 lines, strips off the newline char at the end, and stores them in 2 separate variables. It does not read in the entire file just to get the first 2 strings in it.
with open('text.txt') as f:
word1 = f.readline().strip()
word2 = f.readline().strip()
print word1, word2
# now you can compare word1 and word2 if you like
text.txt
:
foo
bar
asdijaiojsd
asdiaooiasd
Output:
foo bar
EDIT: to make it work with any number of newlines or whitespace:
with open('text.txt') as f:
# sequence of all words in all lines
words = (word for line in f for word in line.split())
# consume the first 2 items from the words sequence
word1 = next(words)
word2 = next(words)
I've verified this to work reliably with various "non-clean" contents of text.txt
.
Note: I'm using generator expressions which are like lazy lists so as to avoid reading more than the needed amount of data. Generator expressions are otherwise equivalent to list comprehensions except they produce items in the sequence lazily, ie as just as much as asked.
with open('text.txt') as f:
lines = [line.strip() for line in f]
print lines[0] == lines[1]
I'm not sure what it is returning but this text file just contains two strings.
Your problem is likely related to whitespace characters (most common being carriage return, linefeed/newline, space and tab). So if you tried to compare your string1
to ' expectedvalue
' and it fails, it's likely because of the newline itself.
Try this: print the length of each string then print each of the actual bytes in each string to see why the comparison fails.
For example:
>>> print len(string1), len(expected)
4 3
>>> for got_character, expected_character in zip(string1, expected):
... print 'got "{}" ({}), but expected "{}" ({})'.format(got_character, ord(got_character), expected_character, ord(expected_character))
...
got " " (32), but expected "f" (102)
got "f" (102), but expected "o" (111)
got "o" (111), but expected "o" (111)
If that's your problem, then you should strip
off the leading and trailing whitespace and then execute the comparison:
>>> string1 = string1.strip()
>>> string1 == expected
True
If you're on a unix-like system, you'll probably have an xxd
or od
binary available to dump a more detailed representation of the file. If you're using windows, you can download many different "hex editor" programs to do the same.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.