对于给定的输入字符串进行查找和替换的最简单方法是什么,比方说abc,并替换为另一个字符串,比如XYZ在文件中/tmp/file.txt?
我正在编写一个应用程序并使用IronPython通过SSH执行命令 - 但我不熟悉Unix,也不知道要查找什么.
我听说Bash除了是命令行界面外,还可以是一种非常强大的脚本语言.所以,如果这是真的,我假设您可以执行这些操作.
我能用bash做什么,以及实现目标的最简单(一行)脚本是什么?
之前已经在不同程度上提出了这类问题,但我觉得它没有以简洁的方式回答,所以我再次提出这个问题.
我想用Python运行一个脚本.让我们说是这样的:
if __name__ == '__main__':
with open(sys.argv[1], 'r') as f:
s = f.read()
print s
Run Code Online (Sandbox Code Playgroud)
获取文件位置,读取它,然后打印其内容.没那么复杂.
好的,那我怎么在C#中运行呢?
这就是我现在拥有的:
private void run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = cmd;
start.Arguments = args;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我通过code.py位置cmd和filename位置,因为args它不起作用.有人告诉我,我应该通过python.exe的cmd,然后code.py filename作为args …
我想从Python代码访问一些用C#编写的.NET程序集.
一点研究表明我有两个选择:
两种解决方案之间的权衡取舍是什么?
我有兴趣在IronPython上运行Django的安装,有没有人在取得一定程度的成功后取得了成功?
如果是这样,请告诉您的经验,表现,提出一些提示,资源和陷阱?
有没有办法从C#调用Python代码,我假设使用IronPython?如果是这样,怎么样?
启动使用程序集的Asp.Net站点时出现以下错误,该程序集又使用dlr和Iron Python进行脚本编写.
BC30560:'ExtensionAttribute'在名称空间'System.Runtime.CompilerServices'中不明确.
问题似乎是众所周知的,问题跟踪器中有一个解决方法.
然而它说它们......
...希望在下一个版本中不需要这种解决方法.
最新版本(我正在使用的版本)是后来的版本,而不是报告中提到的版本.我也尝试下载问题跟踪器中提供的文件并替换当前版本中的文件,但这也不起作用.
除了下载源代码并手动进行构建之外,还有其他解决方案吗?
我在python中编写了一个类,我希望通过IronPython将其包装到.net程序集中并在C#应用程序中实例化.我已经将类迁移到IronPython,创建了一个库程序集并引用它.现在,我如何实际获得该类的实例?
该类看起来(部分)像这样:
class PokerCard:
"A card for playing poker, immutable and unique."
def __init__(self, cardName):
Run Code Online (Sandbox Code Playgroud)
我在C#中编写的测试存根是:
using System;
namespace pokerapp
{
class Program
{
static void Main(string[] args)
{
var card = new PokerCard(); // I also tried new PokerCard("Ah")
Console.WriteLine(card.ToString());
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
为了在C#中实例化这个类,我该怎么办?
我正在尝试做一个简单的hello世界来测试在C#中嵌入IronPython但似乎无法解决这个问题.
这是我的C#文件;
using System;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using System.IO;
public class dynamic_demo
{
static void Main()
{
var ipy = Python.CreateRuntime();
dynamic test = ipy.UseFile(@"../../Test.py");
test.Simple();
}
}
Run Code Online (Sandbox Code Playgroud)
这是python类;
import sys
def Simple():
print 'Hello from Python'
print "Call Dir(): "
print dir()
print "Print the Path: "
print sys.path
Run Code Online (Sandbox Code Playgroud)
我的目标.NET框架是4.0,我正在使用IronPython 2.6 ..
当我运行这个时,我得到2个错误来自一个名为"CSC"的文件; 错误5缺少编译器所需的成员
'Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember'C:\ Users\Tolga\documents\visual studio 2012\Projects\WindowsFormsApplication1\consoleTest\CSC consoleTest
另一个来自我创建的C#文件
错误6无法找到编译动态表达式所需的一种或多种类型.你错过了参考吗?C:\ Users\Tolga\documents\visual studio 2012\Projects\WindowsFormsApplication1\consoleTest\Program.cs 17 9 consoleTest
这是Build的输出
1>------ Build started: Project: consoleTest, Configuration: Debug Any …Run Code Online (Sandbox Code Playgroud) 这很简单,但我喜欢这种漂亮的pythonic方式.基本上,给定一个字典,返回仅包含以某个字符串开头的那些键的子字典.
» d = {'Apple': 1, 'Banana': 9, 'Carrot': 6, 'Baboon': 3, 'Duck': 8, 'Baby': 2}
» print slice(d, 'Ba')
{'Banana': 9, 'Baby': 2, 'Baboon': 3}
Run Code Online (Sandbox Code Playgroud)
这对函数来说相当简单:
def slice(sourcedict, string):
newdict = {}
for key in sourcedict.keys():
if key.startswith(string):
newdict[key] = sourcedict[key]
return newdict
Run Code Online (Sandbox Code Playgroud)
但肯定有一个更好,更聪明,更易读的解决方案?发电机可以帮忙吗?(我从来没有足够的机会使用它们).
任何人都可以分享如何从python代码调用一个简单的C#库(实际上是它的WPF)的工作示例?(我已经尝试过使用IronPython,并且在我的python代码使用不受支持的CPython库时遇到了太多麻烦所以我想尝试反过来并从Python调用我的C#代码).
这是我正在玩的例子:
using System.Runtime.InteropServices;
using System.EnterpriseServices;
namespace DataViewerLibrary
{
public interface ISimpleProvider
{
[DispIdAttribute(0)]
void Start();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class PlotData : ServicedComponent, ISimpleProvider
{
public void Start()
{
Plot plotter = new Plot();
plotter.ShowDialog();
}
}
}
Run Code Online (Sandbox Code Playgroud)
绘图仪是一个绘制椭圆的WPF窗口
我不知道如何从我的python中调用这段代码.有什么建议?
ironpython ×10
python ×7
c# ×5
.net ×4
python.net ×3
bash ×1
cpython ×1
dictionary ×1
django ×1
dynamic ×1
interop ×1
replace ×1
scripting ×1
slice ×1
ssh ×1