Delphi Pos总是返回0

时间:2017-08-21 19:39:55

标签: string web-services delphi indy

我真的不知道为什么Pos会继续返回0而不是char&#34 ;;"字符串中的位置 我必须得到一个php页面的响应,它输出Content-Type:text / plain 所以输出的一个例子就是 2;fulano;fulano;0 3;ciclano;ciclano;0 4;beltrano;beltrano;0 5;foo;foo;0 8;jose;jose;0 9;maria;maria;0

,代码是

var
  linha,uid,login,senha,email,tipo : WideString;
  resposta : TStringList;
  I : Integer;
begin
  try
    resposta := TStringList.Create;
    resposta.Text := frmMain.IdHTTP1.Get(frmMain.cdsConfig.FieldByName('WebService').AsString+'listdest.php');
    for I := 0 to resposta.Count-1 do
    begin
      linha := resposta.Strings[i];
      if i = 0 then
        Delete(linha,1,1); // the first line have one wierd $FEFF
      if length(linha) > 5 then
        begin

          uid := Copy(linha,1,Pos(linha,';')-1);
          Delete(linha,1,Pos(linha,';'));
          login:=Copy(linha,1,Pos(linha,';')-1);
          Delete(linha,1,Pos(linha,';'));
          senha:=Copy(linha,1,Pos(linha,';')-1);
          Delete(linha,1,Pos(linha,';'));
          email:=Copy(linha,1,Pos(linha,';')-1);
          Delete(linha,1,Pos(linha,';'));
          tipo:=Copy(linha,1,Pos(linha,';')-1);
          Delete(linha,1,Pos(linha,';'));
        end;
    end;
    //dlgWait.Close;
  except on E :Exception do
    begin
      MessageBox(Self.Handle,PWideChar(E.Message),'Erro',MB_OK+MB_ICONERROR+MB_APPLMODAL);
      dlgWait.Close;
      FreeAndNil(resposta);
    end;
  end;

1 个答案:

答案 0 :(得分:5)

您对--------------------------------------------------------------------------- Py4JJavaError Traceback (most recent call last) /usr/local/python/pyspark/sql/utils.py in deco(*a, **kw) 62 try: ---> 63 return f(*a, **kw) 64 except py4j.protocol.Py4JJavaError as e: /usr/local/python/lib/py4j-0.10.4-src.zip/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name) 318 "An error occurred while calling {0}{1}{2}.\n". --> 319 format(target_id, ".", name), value) 320 else: Py4JJavaError: An error occurred while calling o864.collectToPython. : org.apache.spark.sql.AnalysisException: Attribute name "Foo Bar" contains invalid character(s) among " ,;{}()\n\t=". Please use alias to rename it.; at org.apache.spark.sql.execution.datasources.parquet.ParquetSchemaConverter$.checkConversionRequirement(ParquetSchemaConverter.scala:581) at org.apache.spark.sql.execution.datasources.parquet.ParquetSchemaConverter$.checkFieldName(ParquetSchemaConverter.scala:567) at org.apache.spark.sql.execution.datasources.parquet.ParquetSchemaConverter$$anonfun$checkFieldNames$1.apply(ParquetSchemaConverter.scala:575) at org.apache.spark.sql.execution.datasources.parquet.ParquetSchemaConverter$$anonfun$checkFieldNames$1.apply(ParquetSchemaConverter.scala:575) at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33) at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186) at org.apache.spark.sql.execution.datasources.parquet.ParquetSchemaConverter$.checkFieldNames(ParquetSchemaConverter.scala:575) at org.apache.spark.sql.execution.datasources.parquet.ParquetFileFormat.buildReaderWithPartitionValues(ParquetFileFormat.scala:293) at org.apache.spark.sql.execution.FileSourceScanExec.inputRDD$lzycompute(DataSourceScanExec.scala:285) at org.apache.spark.sql.execution.FileSourceScanExec.inputRDD(DataSourceScanExec.scala:283) at org.apache.spark.sql.execution.FileSourceScanExec.inputRDDs(DataSourceScanExec.scala:303) at org.apache.spark.sql.execution.ProjectExec.inputRDDs(basicPhysicalOperators.scala:42) at org.apache.spark.sql.execution.WholeStageCodegenExec.doExecute(WholeStageCodegenExec.scala:386) at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117) at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117) at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:138) at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151) at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:135) at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:116) at org.apache.spark.sql.execution.SparkPlan.getByteArrayRdd(SparkPlan.scala:228) at org.apache.spark.sql.execution.SparkPlan.executeTake(SparkPlan.scala:311) at org.apache.spark.sql.execution.CollectLimitExec.executeCollect(limit.scala:38) at org.apache.spark.sql.Dataset$$anonfun$collectToPython$1.apply$mcI$sp(Dataset.scala:2803) at org.apache.spark.sql.Dataset$$anonfun$collectToPython$1.apply(Dataset.scala:2800) at org.apache.spark.sql.Dataset$$anonfun$collectToPython$1.apply(Dataset.scala:2800) at org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId(SQLExecution.scala:65) at org.apache.spark.sql.Dataset.withNewExecutionId(Dataset.scala:2823) at org.apache.spark.sql.Dataset.collectToPython(Dataset.scala:2800) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244) at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357) at py4j.Gateway.invoke(Gateway.java:280) at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132) at py4j.commands.CallCommand.execute(CallCommand.java:79) at py4j.GatewayConnection.run(GatewayConnection.java:214) at java.lang.Thread.run(Thread.java:748) During handling of the above exception, another exception occurred: AnalysisException Traceback (most recent call last) <ipython-input-37-9d7c55a5465c> in <module>() ----> 1 spark.sql("SELECT `Foo Bar` as hey FROM df limit 10").take(1) /usr/local/python/pyspark/sql/dataframe.py in take(self, num) 474 [Row(age=2, name=u'Alice'), Row(age=5, name=u'Bob')] 475 """ --> 476 return self.limit(num).collect() 477 478 @since(1.3) /usr/local/python/pyspark/sql/dataframe.py in collect(self) 436 """ 437 with SCCallSiteSync(self._sc) as css: --> 438 port = self._jdf.collectToPython() 439 return list(_load_from_socket(port, BatchedSerializer(PickleSerializer()))) 440 /usr/local/python/lib/py4j-0.10.4-src.zip/py4j/java_gateway.py in __call__(self, *args) 1131 answer = self.gateway_client.send_command(command) 1132 return_value = get_return_value( -> 1133 answer, self.gateway_client, self.target_id, self.name) 1134 1135 for temp_arg in temp_args: /usr/local/python/pyspark/sql/utils.py in deco(*a, **kw) 67 e.java_exception.getStackTrace())) 68 if s.startswith('org.apache.spark.sql.AnalysisException: '): ---> 69 raise AnalysisException(s.split(': ', 1)[1], stackTrace) 70 if s.startswith('org.apache.spark.sql.catalyst.analysis'): 71 raise AnalysisException(s.split(': ', 1)[1], stackTrace) AnalysisException: 'Attribute name "Foo Bar" contains invalid character(s) among " ,;{}()\\n\\t=". Please use alias to rename it.;' 的来电是倒退的。参数是:

Pos

但是你的代码假定它们是:

function Pos(const SubStr, Str: _ShortStr; Offset: Integer): Integer;

所以它实际上要做的是在function Pos(const Str, SubStr: _ShortStr; Offset: Integer): Integer; 内寻找linha的价值,当然除非';' = linha,否则返回';'

另一种表达方式,正如鲁迪所说,而不是在大海捞针中寻找针头,你的代码正在寻找针头中的大海捞针。

将第一个和第二个参数切换到这些调用。

另一方面,只是性能提示。不要为每个调用0两次,而是保留值的缓存副本...

Pos