基本上我需要解压缩.zip文件,其中包含一个名为modeled的文件夹,该文件夹又包含许多excel文件.
我有幸找到已编写的代码(ZipArchive),这是为了解压缩zip文件,但我无法弄清楚为什么它在我使用它时会抛出错误信息.ZipArchive的代码和错误消息如下所示:
import java.io.{OutputStream, InputStream, File, FileOutputStream}
import java.util.zip.{ZipEntry, ZipFile}
import scala.collection.JavaConversions._
object ZipArchive {
val BUFSIZE = 4096
val buffer = new Array[Byte](BUFSIZE)
def unZip(source: String, targetFolder: String) = {
val zipFile = new ZipFile(source)
unzipAllFile(zipFile.entries.toList, getZipEntryInputStream(zipFile)_, new File(targetFolder))
}
def getZipEntryInputStream(zipFile: ZipFile)(entry: ZipEntry) = zipFile.getInputStream(entry)
def unzipAllFile(entryList: List[ZipEntry], inputGetter: (ZipEntry) => InputStream, targetFolder: File): Boolean = {
entryList match {
case entry :: entries =>
if (entry.isDirectory)
new File(targetFolder, entry.getName).mkdirs
else
saveFile(inputGetter(entry), new FileOutputStream(new File(targetFolder, entry.getName)))
unzipAllFile(entries, inputGetter, …Run Code Online (Sandbox Code Playgroud) 我有以下类与变量from,to和rate.from是一个关键字.如果我想在下面的init方法中使用它,那么编写它的正确方法是什么?
更多上下文:类需要from显式变量,因为它是由另一个开发人员用不同语言编写的POST端点所需的json的一部分.因此,改变变量名是不可能的.
class ExchangeRates(JsonAware):
def __init__(self, from, to, rate):
self.from = from
self.to = to
self.rate = rate
Run Code Online (Sandbox Code Playgroud)
JsonAware代码:
class PropertyEquality(object):
def __eq__(self, other):
return (isinstance(other, self.__class__) and self.__dict__ == other.__dict__)
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, ', '.join(['%s=%s' % (k, v) for (k, v) in self.__dict__.items()]))
class JsonAware(PropertyEquality):
def json(self):
return json.dumps(self, cls=GenericEncoder)
@classmethod
def from_json(cls, json):
return cls(**json)
Run Code Online (Sandbox Code Playgroud)
GenericEncoder代码:
class GenericEncoder(json.JSONEncoder):
def …Run Code Online (Sandbox Code Playgroud) 我试图将坐标“(x1,y1),(x2,y2)”的字符串转换为[[x1,y1],[x2,y2]],其中所有坐标也都转换为浮点数。
我当前的代码似乎没有修剪y坐标的空格,也没有将字符串转换为浮点数。
coordinates = "(-89.38477, 26.6671), (-89.38477, 27.13737), (-88.81348, 27.13737), (-88.81348, 26.6671)"
for x in re.findall("\((.*?)\)", coordinates):
final_coordinates = x.lstrip().split(',')
Run Code Online (Sandbox Code Playgroud) 尝试了不同数量的组合,没有一个可用.
基本上这是我拥有的一串数字:
Run Code Online (Sandbox Code Playgroud)20101002 100224 1 1044 45508 1001 1002 1003 1004 1005 1006
输出我之后:
20101002 100224 1 1044 45508 1001 1002 1003 1004 1005 1006
Run Code Online (Sandbox Code Playgroud)
基本上,必须修剪最后一个数字之前和之后的所有空格.最重要的是,数字之间应该只有一个空格.
我应该补充一点,我目前拥有的代码如下,只处理字符串开头和结尾的空格:
row = line.strip().split(' ')
Run Code Online (Sandbox Code Playgroud)
非常感谢任何帮助,谢谢!