简体   繁体   English

Python需要一个字符缓冲区对象

[英]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 startend是整数而不是字符串...您不能在整数上分割字符串

"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 . startendint ,它们不能作为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: 只需用day的日期字段只是给你一个数字,不是一个完整的日期:

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

This probably isn't enough data for parsing. 这可能是不足以进行解析的数据。 Look into date formatting in Python. 查看Python中的日期格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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