0

I have a .txt file and I want to search for a particular number in that file and get the following couple of characters. The number I want to find in the txt file comes from another txt file called "htmlBetId.txt". I am struggling to make the following code work:

RawFile = open("htmlraw2.txt","rt")
searchfile = RawFile.readline()
betFile = open("htmlBetId.txt","rt")
betID = betFile.readline()
betIDTest =str("'"+'"betId":'+betID[:8]+"'")
print(betIDTest)
                
left,sep,right = searchfile.partition(betIDTest)
print(sep)

When I run the code I get something like this (because of the print):

'"betId":88986745'

In this form, it can't find it.

Now if I write this in the previous code:

left,sep,right = searchfile.partition('"betId":88986745')

It can find it. The problem is that the number after "betId": changes so I have to make the first code work.

If you want the code and the txt files I can post them if you want. Thanks for your help!

wwii
  • 23,232
  • 7
  • 37
  • 77
  • You should consider [opening files using the with keyword](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files). – wwii Oct 21 '20 at 15:23
  • Try: `fmt_string = '"betId":{}'; betIDTest = fmt_string.format(betID[:8])` – wwii Oct 21 '20 at 15:36
  • Related: [String formatting in Python 3](https://stackoverflow.com/questions/13945749/string-formatting-in-python-3), [Using variables in the format() function in Python](https://stackoverflow.com/questions/32413109/using-variables-in-the-format-function-in-python). The documentation: [https://docs.python.org/3/library/string.html#formatstrings](https://docs.python.org/3/library/string.html#formatstrings) – wwii Oct 21 '20 at 15:41
  • [https://stackoverflow.com/questions/52155591/how-to-insert-string-into-a-string-as-a-variable](https://stackoverflow.com/questions/52155591/how-to-insert-string-into-a-string-as-a-variable) – wwii Oct 21 '20 at 15:47
  • Thank you so much! the fmt_string line worked! Awesome! – skyblade420 Oct 24 '20 at 14:01

0 Answers0