Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将pascal转换为python_Python - Fatal编程技术网

将pascal转换为python

将pascal转换为python,python,Python,我有以下Pascal例程: function TForm1.ldExtractFromLine (ldline: String; Post: Integer): String; var s: String; t: array[1..15] of String; i, iT: Integer; begin s := Trim(ldline); iT := 1; while s <> '' do begin s := Trim(s) + ' ';

我有以下Pascal例程:

function TForm1.ldExtractFromLine (ldline: String; Post: Integer): String;
var
  s: String;
  t: array[1..15] of String;
  i, iT: Integer;
begin
  s := Trim(ldline);
  iT := 1;
  while s <> '' do
  begin
    s := Trim(s) + ' ';
    i := 1;
    while s[i] <> ' ' do Inc(i);
    t[iT] := Copy(s, 1, i-1);
    Inc(iT);
    s := Copy(s, i, Length(s));
  end;
  ldExtractFromLine := '';
  if Post < iT then ldExtractFromLine := t[Post];
end;
更多输入数据示例可在此处找到:


代码中的错误在以下行中:

s = s[i:(i+len(s)+1)]
您在字符串
s
中留下一个前导空格,导致无限循环,并且
(i+len(s)+1)
部分出错。可以用以下内容代替:

s = s[i+1:]
通常,此pascal函数只需拆分字符串并获取
Post
-th元素(基于1)。等价物如下:

def ldExtractFromLine (ldline, post):
    a = ldline.split()
    if post <= len(a):
        return a[post-1]
    return ''   
def ldExtractFromLine(ldline,post):
a=ldline.split()

如果发布此代码,它应该做什么?什么是正确的结果?
如果不是完全错误的话,至少看起来微不足道。看到了吗?您实际上是在尝试拆分空格上的字符串吗
ldline.split()
可以做到这一点。如果
ldline.split()[post-1]
能够满足您的需要,则无需真正定义函数。如果程序需要提取多个帖子,那么它可能应该只拆分一次
ldline
,并将值保存在一个数组中,但我们当然不知道这种重构是否可行或必要。
s = s[i+1:]
def ldExtractFromLine (ldline, post):
    a = ldline.split()
    if post <= len(a):
        return a[post-1]
    return ''