exists

Check if a given key already exists in a dictionary

匿名 (未验证) 提交于 2019-12-03 09:06:55
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I wanted to test if a key exists in a dictionary before updating the value for the key. I wrote the following code: if 'key1' in dict.keys(): print "blah" else: print "boo" I think this is not the best way to accomplish this task. Is there a better way to test for a key in the dictionary? 回答1: in is the intended way to test for the existence of a key in a dict . d = dict() for i in xrange(100): key = i % 10 if key in d: d[key] += 1 else: d[key] = 1 If you wanted a default, you can always use dict.get() : d = dict() for i in xrange(100): key

Writing to PostgreSQL from pandas: AttributeError: 'Engine' object has no attribute 'cursor'

匿名 (未验证) 提交于 2019-12-03 09:02:45
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to write a table to a PostgreSQL database from a Pandas data frame (following this answer) but I am getting the error AttributeError: 'Engine' object has no attribute 'cursor' My code is: import pandas as pd from sqlalchemy import create_engine import numpy as np df = pd.DataFrame(index=np.arange(1, 11), data=np.random.random(size=(10, 10)), columns=['c{}'.format(i) for i in np.arange(1, 11)]) engine = create_engine('postgresql://user@localhost:5432/db') df.to_sql('scores', engine) engine can connect to the db and return table

xpath find if node exists

匿名 (未验证) 提交于 2019-12-03 08:57:35
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Using a xpath query how do you find if a node (tag) exists at all? For example if I needed to make sure a website page has the correct basic structure like /html/body and /html/head/title 回答1: ... so for example body node exists body node missing 回答2: Try the following expression: boolean(path-to-node) 回答3: Patrick is correct, both in the use of the xsl:if , and in the syntax for checking for the existence of a node. However, as Patrick's response implies, there is no xsl equivalent to if-then-else, so if you are looking for something more

Getting error “Your InputStream was neither an OLE2 stream, nor an OOXML stream” when created file through apache POI

匿名 (未验证) 提交于 2019-12-03 08:56:10
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to check if my excel file already exists. If it doesn't exists, I want to create a new one and if it exists I will delete it and create a new one. I wrote following program but I am getting error at line - workbook= WorkbookFactory.create(instream); The error is-> java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:89) at tryIng.main(tryIng.java:84) Here is a program -> try { String filePath= "C:/Users

How to check if file exists in StorageFolder in WinRT [duplicate]

匿名 (未验证) 提交于 2019-12-03 08:56:10
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Possible Duplicate: Check if a file exists in the project in WinRT I'm using the StorageFolder and need to check if a file exists befor I read it to avoid a exception. my code looks like this: StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFile sampleFile = await storageFolder.GetFileAsync(myPath); the problem is, I can't find a method which checks if a file exist 回答1: Last time I checked you had to catch an exception: (might have changed) Edit: this is one way to do it :) Like so: static async Task<bool>

Test if value exists in several lists

匿名 (未验证) 提交于 2019-12-03 08:52:47
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I would like to check if a value exists in every list. The following returns True as expected, but seems un-pythonic. What is the correct/more elegant way to do this? a = [1 ,2] b = [1, 3] c = [1, 4] d = [2, 5] False in [True if 1 in l else False for l in [a, b, c, d] ] 回答1: You can use all and a generator expression : all(1 in x for x in (a, b, c, d)) Demo: >>> a = [1 ,2] >>> b = [1, 3] >>> c = [1, 4] >>> d = [2, 5] >>> all(1 in x for x in (a, b, c, d)) False >>> all(1 in x for x in (a, b, c)) True >>> In addition to being more readable,

Idempotent PostgreSQL DDL scripts

匿名 (未验证) 提交于 2019-12-03 08:52:47
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm looking for a way to script postgreSQL schema changes in an idempotent manner. In MSSQL I could do something like this: if(not exists(select * from information_schema.columns where table_name = 'x' and column_name = 'y')) begin alter table x add y int end go PostgreSQL doesn't seem to allow ad-hoc pl/pgsql in the same way MSSQL does with T-SQL so I can't use control structures in a SQL script and run it with psql -f x.sql. I know PostgreSQL will throw an error if the object already exists but I don't want to have to ignore errors. I

How can I programmatically determine if a table exists within a SQL Server CE database?

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Back when I only had one table in my .sdf file, this code worked fine: const string sdfPath = @"\Program Files\duckbilled\Platypus.sdf"; string dataSource = string.Format("Data Source={0}", sdfPath); if (!File.Exists(sdfPath)) { using (var engine = new SqlCeEngine(dataSource)) { engine.CreateDatabase(); } using (var connection = new SqlCeConnection(dataSource)) { connection.Open(); using (var command = new SqlCeCommand()) { command.Connection = connection; command.CommandText = "CREATE TABLE Platydudes (Id int NOT NULL, BillSize smallint NOT

csproj copy files depending on operating system

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using .NET Core to build a cross platform class library. Depending on the operating system that the C# .NET Core project is built for using a .csproj file, I need to copy a native library to the project's output directory. E.g., for OS X I want to copy a .dylib file, for Windows I want to copy a .DLL file, for Linux I want to copy a .so file. How can I do this with a Condition clause in a .csproj ItemGroup? <ItemGroup> <Content Include="libNative.dylib" Condition=" '$(Configuration)|$(Platform)' == 'Debug|OSX' "> <CopyToOutputDirectory

Amazon AWSClientFactory does not exists

匿名 (未验证) 提交于 2019-12-03 08:46:08
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I created an empty .Net Core application and installed nuget packages for Both Amazon.Core and Amazon.S3. Then I tried to use S3 to get an object but I'm stuck at the very first moment... Amazon.AWSClientFactory is nowhere to be found inside the assembly. Even with dotPeek I tried to search this factory method but I couldn't find it. Even the sample code from Amazon doesn't work. Where I supposed to find this class ? 回答1: Amazon.Core and Amazon.S3 are part of the AWS SDK for .NET v3. Per the AWS SDK for .NET Version 3 Migration Guide :