0

I'm trying to make a script that reads a file, finds today's and yesterday's date and then prints all of the content between those two dates. However whenever I try to run this, I get expected a character buffer object on the last line.

import datetime
import re
today = datetime.date.today().day
yesterday = (today - 1)
file=open("test.txt","r")
s = file.read()
start = today
end = yesterday

print((s.split(start))[1].split(end)[0])
3
  • 2
    .split() expects a string, not an integer. Commented Mar 24, 2016 at 22:37
  • What are you trying to accomplish? Commented Mar 24, 2016 at 22:39
  • There's no need for the extra parentheses in (s.split(start))[1], and there's already a lot of parentheses, especially if you add str, so they're worth removing. Just write s.split(start)[1]. Commented Mar 24, 2016 at 23:19

2 Answers 2

1

start and end are integers not strings ... you cannot split a string on an integer

"some5string".split(5) # wont work it needs a string
"some5string".split("5") # will work

change it to

print((s.split(str(start)))[1].split(str(end))[0])
Sign up to request clarification or add additional context in comments.

Comments

0

start and end are ints which cannot be passed as arguments to split.

>>> "234".split(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected a character buffer object

You could convert them to strings but I don't really think that's enough. Simply using the day field of a date just gives you a number, not a full date:

>>> datetime.date.today().day
25

This probably isn't enough data for parsing. Look into date formatting in Python.

2 Comments

Thanks, but for the files I'm using the dates are just numbers, not complete dates :)
Well then I hope you're being really careful and preferably only running this once because there's all sorts of ways that could go wrong. For example all the other data in the files shouldn't contain numbers, and the files can only contain certain combinations of dates (e.g. if the file contains data from the 29th of Feb to the 2nd of March, a space of just 3 days, you can expect bugs).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.