简体   繁体   中英

Python expected a character buffer object

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])

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])

start and end are int s 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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM