如何在Python中复制C#.read(buffer,0,buffer.Length) [英] How to replicate C# .read(buffer, 0, buffer.Length) in Python

查看:467
本文介绍了如何在Python中复制C#.read(buffer,0,buffer.Length)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我问了一个关于文件流方法的类似问题, C#中的FileStream与Python等效的是什么?,但我现在意识到我应该可能会询问.read函数。

Yesterday I asked a similar question about the filestream method, What is the Python equivalent to FileStream in C#?, but I now realize I should have probably been asking about the .read function instead.

对于上下文我试图使用soap API的流式响应,它应该输出一个CSV文件。响应输出一个以base 64编码的字符串,我不知道该怎么做。此外,api文档说必须将响应读取到目标缓冲区。

For context I am trying to consume a streamed response from a soap API, which should output a CSV file. The response outputs a string coded in base 64, which I do not know what to do with. Also the api documentation says that the response must be read to a destination buffer-by-buffer.

以下是代码中的上下文。该代码由api的文档提供:

Here is the context in the code. The code was provided by the api's documentation:

byte[] buffer = new byte[4000];
bool endOfStream = false;
int bytesRead = 0;
using (FileStream localFileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write))
{
   using (Stream remoteStream = client.DownloadFile(jobId, chkFormatAsXml.Unchecked))
   {
     while (!endOfStream)
     {
         bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
         if (bytesRead > 0)
         {
              localFileStream.Write(buffer, 0, bytesRead);
              totalBytes += bytesRead;
         }
         else
         {
              endOfStream = true;
         }
      }
   }
}

任何非常感谢帮助,即使它只是指向正确的方向,因为我现在非常迷失。我还有另一个问题,现在引用同样的问题。 写入流式响应(文件 - 像对象)到CSV文件Byte by Byte in Python

Any help would be greatly appreciated, even if it is just to point me in the right direction, as I am very lost now. I also, had another question referencing the same problem today as well. Write Streamed Response(file-like object) to CSV file Byte by Byte in Python

推荐答案

它看起来像 bytesread = len(openfile.read(4000))是正确的复制方法 bytesRead = remoteStream.Read(buffer,0,buffer.Length);

It looks like bytesread = len(openfile.read(4000)) is the correct way to replicate bytesRead = remoteStream.Read(buffer, 0, buffer.Length);

谢谢DYZ

这篇关于如何在Python中复制C#.read(buffer,0,buffer.Length)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆