python - Python 问题:不支持的格式,或损坏的文件:预期的 BOF 记录;找到 b'\n
问题描述
以下代码不起作用并显示错误。任何人都可以提出任何解决方案吗?请注意,excel 文件格式为 xls。当我手动打开文件时,它会显示以下消息。
问候, 埃克拉姆
import xlrd
file=xlrd.open_workbook("C:\\PzcharmProjects\\Track and Trace - Single Booking.xls")
sheet=file.sheet_by_index(0)
print(file.nrows)
输出:
---------------------------------------------------------------------------
XLRDError Traceback (most recent call last)
<ipython-input-2-2279c402499e> in <module>
1 import xlrd
----> 2 file=xlrd.open_workbook("C:\\PzcharmProjects\\Track and Trace - Single Booking.xls")
3 sheet=file.sheet_by_index(0)
4 print(file.nrows)
C:\Anaconda\lib\site-packages\xlrd\__init__.py in open_workbook(filename, logfile, verbosity, use_mmap, file_contents, encoding_override, formatting_info, on_demand, ragged_rows)
146
147 from . import book
--> 148 bk = book.open_workbook_xls(
149 filename=filename,
150 logfile=logfile,
C:\Anaconda\lib\site-packages\xlrd\book.py in open_workbook_xls(filename, logfile, verbosity, use_mmap, file_contents, encoding_override, formatting_info, on_demand, ragged_rows)
90 t1 = perf_counter()
91 bk.load_time_stage_1 = t1 - t0
---> 92 biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
93 if not biff_version:
94 raise XLRDError("Can't determine file's BIFF version")
C:\Anaconda\lib\site-packages\xlrd\book.py in getbof(self, rqd_stream)
1276 bof_error('Expected BOF record; met end of file')
1277 if opcode not in bofcodes:
-> 1278 bof_error('Expected BOF record; found %r' % self.mem[savpos:savpos+8])
1279 length = self.get2bytes()
1280 if length == MY_EOF:
C:\Anaconda\lib\site-packages\xlrd\book.py in bof_error(msg)
1270
1271 def bof_error(msg):
-> 1272 raise XLRDError('Unsupported format, or corrupt file: ' + msg)
1273 savpos = self._position
1274 opcode = self.get2bytes()
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\n<html x'
iOS Swift URLSession POST请求因慢速API调用而重复
我有一个下载任务,首先调用一个 REST API,服务器需要为其生成一个相当大的文件,该文件需要几分钟才能生成,因为它是 CPU 和磁盘 IO 密集型的。客户端等待服务器使用它生成的文件的 URL 给出 JSON 响应。文件下载在获得第一个结果后开始。
对于生成特别大文件的调用,这会导致服务器响应速度非常慢,我看到我的代码没有启动的重复请求。
最初在服务器端工作的人告诉我重复的请求。然后我设置了一种检查网络流量的方法。这是通过设置连接到有线网络的 Mac 并启用网络共享并使用Proxyman检查从 iPhone 到 API 服务器的流量来完成的。我在网络层看到同一 API 请求的多个实例,但我的代码从未收到通知。
代码看起来像这样
@objc class OfflineMapDownloadManager : NSObject, URLSessionDelegate, URLSessionDownloadDelegate {
@objc func download(){
let config = URLSessionConfiguration.background(withIdentifier: "OfflineMapDownloadSession")
config.timeoutIntervalForRequest = 500
config.shouldUseExtendedBackgroundIdleMode = true
config.sessionSendsLaunchEvents = true
urlSession = URLSession(configuration: config, delegate: self, delegateQueue: nil)
getMapUrlsFromServer(bounds)
}
func getMapUrlsFromServer(){
var urlString = "http://www.fake.com/DoMakeMap.php"
if let url = URL(string: urlString) {
let request = NSMutableURLRequest(url: url)
//...Real code sets up a JSON body in to params...
request.httpBody = params.data(using: .utf8 )
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.timeoutInterval = 500
urlSession?.configuration.timeoutIntervalForRequest = 500
urlSession?.configuration.timeoutIntervalForResource = 500
request.httpShouldUsePipelining = true
let backgroundTask = urlSession?.downloadTask(with: request as URLRequest)
backgroundTask?.countOfBytesClientExpectsToSend = Int64(params.lengthOfBytes(using: .utf8))
backgroundTask?.countOfBytesClientExpectsToReceive = 1000
backgroundTask?.taskDescription = "Map Url Download"
backgroundTask?.resume()
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
if (downloadTask.taskDescription == "CTM1 Url Download") {
do {
let data = try Data(contentsOf: location, options: .mappedIfSafe)
let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
if let jsonResult = jsonResult as? Dictionary<String, AnyObject> {
if let ctm1Url = jsonResult["CTM1Url"] as? String {
if let filesize = jsonResult["filesize"] as? Int {
currentDownload?.ctm1Url = URL(string: ctm1Url)
currentDownload?.ctm1FileSize = Int32(filesize)
if (Int32(filesize) == 0) {
postDownloadFailed()
} else {
startCtm1FileDownload(ctm1Url,filesize)
}
}
}
}
} catch {
postDownloadFailed()
}
}
}
这个下载类还有更多内容,因为它会在第一次 api 调用完成后下载实际文件。由于问题发生在该代码执行之前,因此我没有将其包含在示例代码中。
Proxyman 的日志显示 API 调用在(分:秒)46:06、47:13、48:21、49:30、50:44、52:06、53:45
发出以刚刚超过 1 分钟的间隔重复。
有一个 API 字段,我可以在其中输入任何值,服务器会将其回显给我。我在那里放置了一个用 CACurrentMediaTime() 生成的时间戳,并登录 Proxyman 显示它确实是相同的 API 调用,所以我的代码不可能被多次调用。似乎 iOS 网络层正在重新发出 http 请求,因为服务器需要很长时间才能响应。这最终导致服务器出现问题并且 API 失败。
任何帮助将不胜感激。
解决方案
推荐阅读
- php - 如何在codeigniter中用新文件替换旧文件?
- javascript - 键入一个通用对象作为输入
- javascript - 由于 JSON 输入错误的意外结束,Nodemon 安装失败
- ruby-on-rails - 如何解决cron作业问题?
- kotlin - navController.currentDestination + findFragmentById 返回 null
- tensorflow - Tflite 200mb 大
- c++ - 从 /analyze 结果中过滤库代码
- java - 前缀树 vs TreeSet
- javascript - 电子签名功能不返回任何内容
- r - 从数据框中的向量中提取最后一个值
问题描述
以下代码不起作用并显示错误。任何人都可以提出任何解决方案吗?请注意,excel 文件格式为 xls。当我手动打开文件时,它会显示以下消息。
问候, 埃克拉姆
import xlrd
file=xlrd.open_workbook("C:\\PzcharmProjects\\Track and Trace - Single Booking.xls")
sheet=file.sheet_by_index(0)
print(file.nrows)
输出:
---------------------------------------------------------------------------
XLRDError Traceback (most recent call last)
<ipython-input-2-2279c402499e> in <module>
1 import xlrd
----> 2 file=xlrd.open_workbook("C:\\PzcharmProjects\\Track and Trace - Single Booking.xls")
3 sheet=file.sheet_by_index(0)
4 print(file.nrows)
C:\Anaconda\lib\site-packages\xlrd\__init__.py in open_workbook(filename, logfile, verbosity, use_mmap, file_contents, encoding_override, formatting_info, on_demand, ragged_rows)
146
147 from . import book
--> 148 bk = book.open_workbook_xls(
149 filename=filename,
150 logfile=logfile,
C:\Anaconda\lib\site-packages\xlrd\book.py in open_workbook_xls(filename, logfile, verbosity, use_mmap, file_contents, encoding_override, formatting_info, on_demand, ragged_rows)
90 t1 = perf_counter()
91 bk.load_time_stage_1 = t1 - t0
---> 92 biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
93 if not biff_version:
94 raise XLRDError("Can't determine file's BIFF version")
C:\Anaconda\lib\site-packages\xlrd\book.py in getbof(self, rqd_stream)
1276 bof_error('Expected BOF record; met end of file')
1277 if opcode not in bofcodes:
-> 1278 bof_error('Expected BOF record; found %r' % self.mem[savpos:savpos+8])
1279 length = self.get2bytes()
1280 if length == MY_EOF:
C:\Anaconda\lib\site-packages\xlrd\book.py in bof_error(msg)
1270
1271 def bof_error(msg):
-> 1272 raise XLRDError('Unsupported format, or corrupt file: ' + msg)
1273 savpos = self._position
1274 opcode = self.get2bytes()
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\n<html x'
我有一个下载任务,首先调用一个 REST API,服务器需要为其生成一个相当大的文件,该文件需要几分钟才能生成,因为它是 CPU 和磁盘 IO 密集型的。客户端等待服务器使用它生成的文件的 URL 给出 JSON 响应。文件下载在获得第一个结果后开始。
对于生成特别大文件的调用,这会导致服务器响应速度非常慢,我看到我的代码没有启动的重复请求。
最初在服务器端工作的人告诉我重复的请求。然后我设置了一种检查网络流量的方法。这是通过设置连接到有线网络的 Mac 并启用网络共享并使用Proxyman检查从 iPhone 到 API 服务器的流量来完成的。我在网络层看到同一 API 请求的多个实例,但我的代码从未收到通知。
代码看起来像这样
@objc class OfflineMapDownloadManager : NSObject, URLSessionDelegate, URLSessionDownloadDelegate {
@objc func download(){
let config = URLSessionConfiguration.background(withIdentifier: "OfflineMapDownloadSession")
config.timeoutIntervalForRequest = 500
config.shouldUseExtendedBackgroundIdleMode = true
config.sessionSendsLaunchEvents = true
urlSession = URLSession(configuration: config, delegate: self, delegateQueue: nil)
getMapUrlsFromServer(bounds)
}
func getMapUrlsFromServer(){
var urlString = "http://www.fake.com/DoMakeMap.php"
if let url = URL(string: urlString) {
let request = NSMutableURLRequest(url: url)
//...Real code sets up a JSON body in to params...
request.httpBody = params.data(using: .utf8 )
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.timeoutInterval = 500
urlSession?.configuration.timeoutIntervalForRequest = 500
urlSession?.configuration.timeoutIntervalForResource = 500
request.httpShouldUsePipelining = true
let backgroundTask = urlSession?.downloadTask(with: request as URLRequest)
backgroundTask?.countOfBytesClientExpectsToSend = Int64(params.lengthOfBytes(using: .utf8))
backgroundTask?.countOfBytesClientExpectsToReceive = 1000
backgroundTask?.taskDescription = "Map Url Download"
backgroundTask?.resume()
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
if (downloadTask.taskDescription == "CTM1 Url Download") {
do {
let data = try Data(contentsOf: location, options: .mappedIfSafe)
let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
if let jsonResult = jsonResult as? Dictionary<String, AnyObject> {
if let ctm1Url = jsonResult["CTM1Url"] as? String {
if let filesize = jsonResult["filesize"] as? Int {
currentDownload?.ctm1Url = URL(string: ctm1Url)
currentDownload?.ctm1FileSize = Int32(filesize)
if (Int32(filesize) == 0) {
postDownloadFailed()
} else {
startCtm1FileDownload(ctm1Url,filesize)
}
}
}
}
} catch {
postDownloadFailed()
}
}
}
这个下载类还有更多内容,因为它会在第一次 api 调用完成后下载实际文件。由于问题发生在该代码执行之前,因此我没有将其包含在示例代码中。
Proxyman 的日志显示 API 调用在(分:秒)46:06、47:13、48:21、49:30、50:44、52:06、53:45 发出以刚刚超过 1 分钟的间隔重复。
有一个 API 字段,我可以在其中输入任何值,服务器会将其回显给我。我在那里放置了一个用 CACurrentMediaTime() 生成的时间戳,并登录 Proxyman 显示它确实是相同的 API 调用,所以我的代码不可能被多次调用。似乎 iOS 网络层正在重新发出 http 请求,因为服务器需要很长时间才能响应。这最终导致服务器出现问题并且 API 失败。
任何帮助将不胜感激。