type-conversion 相关问题

类型转换是隐式或显式地将一种数据类型的实体更改为另一种数据类型的方式。这样做是为了利用类型层次结构或类型表示的某些功能。

将时间列从“对象”转换为“日期时间”数据类型后使用 df,我注意到数据发生变化,并且此变化不在位置

将 pandas 导入为 pd order_details_id order_id order_date order_time item_id 0 1 1 1/1/23 11:38:36 上午 109.0 1 2 2 1/1/23 11:5...

回答 1 投票 0

如何在c#中将字符串转换为双精度值

我正在使用.net框架构建简单的c# winform桌面应用程序。 我需要将“172.5632”这样的字符串转换为双精度值。 我尝试了 Double.parse() 和 Convert.toString() 他们俩...

回答 1 投票 0

Python 中的浮点到分数转换

在练习Python 3.52中的float类型到Fraction类型转换的主题时,我发现了两种不同转换方式之间的区别。 第一种方法是: >>> 来自

回答 2 投票 0

PowerShell op_Explicit 来自仅具有一个属性的对象

我正在处理一个我无法真正理解的奇怪错误。将我的脚本精简到最低限度后,就会发生这样的情况。基本上我正在尝试创建一个支持显式的类

回答 1 投票 0

R:将字符转换为时间戳

目前我们正在实施某种直接营销 我们在某一天从欧洲某个国家发送短信(时间戳),然后收到当地客户的回复

回答 2 投票 0

将存储为 VARCHAR 的数字的二进制表示形式转换为 INT

我正在使用一个 SQL Server 数据库,该数据库有一列包含二进制数。 但是,二进制数存储为 VARCHAR。 我需要将 VARCHAR 转换为其数字表示形式...

回答 1 投票 0

如何在保留 R 中的列的同时从“整数”转换为“双精度”对象类型?

我希望我的数据是“双精度”而不是“整数”对象类别,以便能够使用特定的 R 包,同时保留数据分离为不同的

回答 1 投票 0

为什么 type(bin(1)) 返回字符串?

我正在尝试用Python制作一个国际象棋引擎,我正在尝试将1和0的列表写为二进制值,但我在互联网上找不到任何有用的东西,我正在尝试自己的

回答 1 投票 0

如何将绝对触摸输入转换为鼠标中键单击和拖动?

我购买了 StaffPad,但不幸的是我没有 MS 设备来书写和使用该软件的好处。因此,在电脑上用鼠标书写并不是一种舒适的体验。我尝试在我的

回答 1 投票 0

以安全的方式将字符串转换为整数

我有一个小方法,除此之外还可以将字符串转换为整数。由于该字符串是该方法的参数,我想确保该字符串是可转换的。所以我...

回答 7 投票 0

如何将 AudioSegment 转换为 NumPy 数组并返回?

正如标题所述,我在将 PyDub AudioSegment 转换为 NumPy 数组并返回时遇到了困难。我知道如何将 PyDub AudioSegment 转换为 NumPy 数组,并且对如何...

回答 1 投票 0

无需反序列化的Google Protobuf

通常,google protobuf 或nanopb 实现旨在分别在服务器/客户端上序列化/反序列化消息。 然而,我面临着序列化的情况

回答 1 投票 0

如何加载具有自定义类型和自定义类型列表的字典

我正在开发一个统一项目,您可以在其中保存具有自定义类型 GraphNode 的键的字典,并且每个键的值是相同类型 GraphNode Dictionary 的列表 我正在开发一个统一项目,您可以在其中保存具有自定义类型 GraphNode 的键的字典,并且每个键的值都是相同类型 GraphNode 的列表Dictionary<GraphNode, List<GraphNode>>。我正在使用 NewtonSoft 库将 json 从对象转换为 json 文本以及从文本转换为对象。 游戏数据脚本: using System; using System.Collections.Generic; [Serializable] public class GameData { public Dictionary<GraphNode, List<GraphNode>> allGraphNodes; public GameData() { allGraphNodes = new Dictionary<GraphNode, List<GraphNode>>(); } } GraphNode 类: public class GraphNode { public enum Type { entrance = 0, enemy = 1, hub = 2, shop = 3, boss = 4 } public Type type; public GraphNode(Type type2) { SetType(type2); } public void SetType(Type type2) { type = type2; } } 我在stackoverflow上尝试了类似的例子,但它不起作用,这是为了保存: public void Save(GameData data) { string text = Path.Combine(dataDirPath, dataFileName); try { Directory.CreateDirectory(Path.GetDirectoryName(text)); string text3 = JsonConvert.SerializeObject(data, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple }); if (useEncryption) { text3 = EncryptDecrypt(text3); } using FileStream stream = new FileStream(text, FileMode.Create); using StreamWriter streamWriter = new StreamWriter(stream); streamWriter.Write(text3); } catch (Exception ex) { Debug.LogError("Error" + text + "\n" + ex); } } 保存的字典示例如下所示: { "$type": "GameData, Assembly-CSharp", "allGraphNodes": { "$type": "System.Collections.Generic.Dictionary`2[[GraphNode, Assembly-CSharp],[System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib]], mscorlib", "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 0 }, { "$type": "GraphNode, Assembly-CSharp", "type": 3 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 3 }, { "$type": "GraphNode, Assembly-CSharp", "type": 2 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 }, { "$type": "GraphNode, Assembly-CSharp", "type": 4 }, { "$type": "GraphNode, Assembly-CSharp", "type": 2 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 4 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 3 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 }, { "$type": "GraphNode, Assembly-CSharp", "type": 3 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 }, { "$type": "GraphNode, Assembly-CSharp", "type": 3 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 4 } ] } } } 这用于加载(出现错误的部分被标记为重要行): public GameData LoadLayout() { string text = Path.Combine(dataDirPath, dataFileName); GameData result = null; if (File.Exists(text)) { try { string text2 = ""; using (FileStream stream = new FileStream(text, FileMode.Open)) { using StreamReader streamReader = new StreamReader(stream); text2 = streamReader.ReadToEnd(); Debug.Log(text2); } if (useEncryption) { text2 = EncryptDecrypt(text2); } result = JsonConvert.DeserializeObject<GameData>(text2, new JsonSerializerSettings //important line { TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple }); return result; } catch (Exception ex) { Debug.LogError("Error" + text + "\n" + ex); return result; } } return result; } 错误打印: Newtonsoft.Json.JsonSerializationException:无法将字符串“GraphNode”转换为字典键类型“GraphNode”。创建一个 TypeConverter 将字符串转换为键类型对象。路径“allGraphNodes.GraphNode”,第 5 行,位置 16。 ---> Newtonsoft.Json.JsonSerializationException:将值“GraphNode”转换为类型“GraphNode”时出错。路径“allGraphNodes.GraphNode”,第 5 行,位置 16。 ---> System.ArgumentException:无法从 System.String 转换或转换为 GraphNode。 您遇到几个问题: 您的字典具有复杂的键,但是,正如文档中所述,由于 Json.NET 默认将字典序列化为 JSON 对象,因此序列化仅适用于可转换为字符串的键。 因此,您需要将 allGraphNodes 序列化为数组。 将字典序列化为(键值对)数组中显示的方法有多种,但最简单的可能是使用代理数组属性序列化字典。 字典中的 List<GraphNode> 值可能引用字典中的其他键。要在序列化过程中保留这些引用,您需要在启用引用保留的情况下进行序列化。 不幸的是,引用保存不适用于参数化构造函数,因此GraphNode将需要一个无参数构造函数。只要标上[JsonConstructor]就可以私密。 您正在使用TypeNameHandling.All进行序列化,但是正如文档中所述,这会带来安全风险。有关详细信息,请参阅 Newtonsoft Json 中的 TypeNameHandling 警告 和 由于 Json.Net TypeNameHandling auto?,外部 json 容易受到攻击?它还可能导致 JSON 文件出现不必要的膨胀。 您当前的数据模型不需要此设置,因为它不使用多态性。如果您确实认为有必要,请考虑编写自定义的序列化绑定器。 将所有这些放在一起,如果您按如下方式修改数据模型: [Serializable] public class GameData { [JsonIgnore] public Dictionary<GraphNode, List<GraphNode>> allGraphNodes; public GameData() { allGraphNodes = new Dictionary<GraphNode, List<GraphNode>>(); } [JsonProperty("allGraphNodes")] KeyValuePair<GraphNode, List<GraphNode>> [] allGraphNodeArray { get { return allGraphNodes?.ToArray(); } set { allGraphNodes = allGraphNodes ?? new Dictionary<GraphNode, List<GraphNode>>(); if (value != null) foreach (var pair in value) allGraphNodes.Add(pair.Key, pair.Value); } } } [Serializable] [JsonObject(IsReference = true)] // Forces all instances to be serialized with reference preservation public class GraphNode { public enum Type { entrance = 0, enemy = 1, hub = 2, shop = 3, boss = 4 } public Type type; [JsonConstructor] // Constructor for serialization. GraphNode() { } public GraphNode(Type type) { SetType(type); } public void SetType(Type type) { this.type = type; } } 您将能够按如下方式进行序列化: var settings = new JsonSerializerSettings { // Add any settings as required, e.g. // Converters = { new StringEnumConverter() }, }; var json1 = JsonConvert.SerializeObject(gameData1, settings); var gameData2 = JsonConvert.DeserializeObject<GameData>(json1, settings); 演示小提琴在这里。

回答 1 投票 0

在 PostgreSQL 中将整数转换为字符串

如何将整数转换为字符串作为 PostgreSQL 查询的一部分? 因此,举例来说,我需要: SELECT * FROM table WHERE = '数字字符串' 可以在哪里

回答 5 投票 0

numpy 数组在写入文件时更改为字符串

我有一个数据框,其中一列是 numpy 数组: DF 名称 向量 0 阿贝纳基石-(Ce) [0.0, 0.0, 0.0, 0.0, 0.0, 0.043, 0.0, 0.478, 0... 1 阿伯纳西石 [0.0...

回答 2 投票 0

将字符串转换为二进制的最快方法?

我想使用字符串类将字符串转换为二进制。一个字符一个字符地执行此操作的快速方法是什么?环形?或者有什么函数可以为我转换吗? 1 和 0

回答 4 投票 0

用户定义的转换函数和转换为引用

我遇到以下代码的编译错误: 符号组类 { std::字符串 d_; 民众: SymbolGroup(std::string a):d_(a){} // 显式运算符 const std::string&() c...

回答 3 投票 0

为什么这个函数调用不明确,转换为 bool 需要额外的步骤?

考虑以下代码: 结构体Foo { typedef bool (*fptr_t)(int); 运算符 fptr_t() const { 返回_func; } fptr_t_func; }; 类包装器 { 民众: 包装纸(...

回答 1 投票 0

无需反序列化的 Google Protobuf

通常 google protobuf 或 nanopb 实现旨在分别在服务器端和客户端上序列化/反序列化消息。 但是,我面临着序列化的情况......

回答 1 投票 0

C++ 将向量<int>转换为向量<double>

将 std::vector intVec 转换为 std::vector doubleVec 的好干净方法是什么。或者,更一般地说,转换两个可转换类型的向量?

回答 3 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.