4

问题

当前数据

........Column 1....Column 2.......Column3....Column 4

Row1...........0...........0.............0...........Y    
Row2.......3142.56...........500............0...........N    
Row3.......3142.56...........500............0...........N

源文件具有固定宽度列导出固定宽度列的程序不包括小数点后的数字作为保留的固定宽度大小的一部分

  • 第 1 行是正常输出,工作正常
  • 第 2 行和第 3 行有 2 个小数位,因此第 2、3、4 列都被推出了 2 个位置。

我创建了一个 C# 脚本来重写文件并尝试解决此问题。

我找到了一种读取行并分成列的方法。这成为一个字符串变量。但是需要确定字符串是否包含“0-9”后跟“.”。图案。然后我需要计算模式后面有多少个小数。然后删除 X 数量的空白(开头的小数位数)。

所以

当前状态 [_ _ _ _ _3142.56]

我们想在 [_ _ _3142.56] 之后看到什么

到目前为止的尝试 到目前为止,我已经能够发现 Regex 似乎做了我所追求的。那么 IndexOf(".").length 可以用来统计小数点后的位数。

所以我想出了以下

        // Resolve Decimal Issues
        foreach (object Column in splitLine)
        {
            String CurrentColumn = Column.ToString();

            if (Regex.Match(CurrentColumn, @"^[0-9]+(\.[0-9]+)?$").Success == true)
            {
                // Count how many numbers AFTER a decimal
                int decimalLength = CurrentColumn.Substring(CurrentColumn.IndexOf(".")).Length;
                if (decimalLength >= 1)
                {
                    // Remove this amount of places from the start of the string
                    CurrentColumn = CurrentColumn.Substring(CurrentColumn.Length - decimalLength);
                }
            }

             //Start re-joining the string
            newLine = newLine + CurrentColumn + "\t";
        }

问题是 IndexOf 在找不到匹配项时返回 -1,从而导致错误。

错误堆栈

Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. 
---> System.ArgumentOutOfRangeException: StartIndex cannot be less than zero.

Parameter name: startIndex
   at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
   at ST_dd38f3d289db4495bf07257723356ed3.csproj.ScriptMain.Main()

   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

所以我对我能做些什么来解决这个问题有点困惑。我认为我在正确的道路上.. 但是最后一个错误让我有点迷茫。

4

3 回答 3

2

我觉得你的逻辑有问题。

给定bbbb123.45(b是一个空格),您的逻辑将给出decimalLength3。CurrentColumn.Substring(CurrentColumn.Length - decimalLength)将返回.45

你真正想要的是CurrentColumn.Substring(decimalLength),它将从第三个字符开始并返回b123.45

方法大致相同:

    // Resolve Decimal Issues
    foreach (object Column in splitLine)
    {
        String CurrentColumn = Column.ToString();

        if (Regex.IsMatch(CurrentColumn, @"^[0-9]+(\.[0-9]+)?$"))
        {
            // If there's a decimal point, remove characters from the front
            // of the string to compensate for the decimal portion.
            int decimalPos = CurrentColumn.IndexOf(".");
            if (decimalPos != -1)
            {
                CurrentColumn = CurrentColumn.Substring(CurrentColumn.Length - decimalPos);
            }
        }

         //Start re-joining the string
        newLine = newLine + CurrentColumn + "\t";
    }

顺便说一句,如果小数部分的长度超过了字符串前面的空格数,那么这将非常失败。根据你的描述,我认为这不是问题。但这是要记住的事情。

于 2013-06-19T16:09:41.580 回答
0

以下是一种简短、密集和 LINQed 的方法。无需寻找任何东西,只需拆分、打包、填充和重建。这实际上(我刚刚注意到)适用于任何要制作为固定宽度的文本文件。

// "inputData" is assumed to contain the whole source file

const int desiredFixedWidth = 12; // How wide do  you want your columns ?
const char paddingChar = ' '; // What char do you want to pad your columns with?

// Step 1: Split the lines
var srcLines = inputData.Split(new string[]{Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);

// Step 2: Split up each line, ditch extra chars, pad the values, rebuild the file
var outLines = srcLines.Select(s => 
    string.Join(paddingChar.ToString(), 
        s.Split(new string[] { paddingChar.ToString() }, StringSplitOptions.RemoveEmptyEntries)
            .Select(l => l.PadLeft(desiredFixedWidth, paddingChar))));

附带说明一下,损坏文件的“生成器”需要修复以符合您想要的宽度......

于 2013-06-19T10:48:59.367 回答
0

尝试这个:

// Resolve Decimal Issues
foreach (object Column in splitLine)
{
    String CurrentColumn = Column.ToString();
    char[] s = {'.'};

    if (Regex.Match(CurrentColumn, @"^[0-9]+(\.[0-9]+)?$").Success && CurrentColumn.Contains('.'))
        {
            // Count how many numbers AFTER a decimal
            int decimalLength = CurrentColumn.split(s, StringSplitOptions.None)[1].Length;
            if (decimalLength >= 1)
            {
                // Remove this amount of places from the start of the string
                CurrentColumn = CurrentColumn.Substring(CurrentColumn.Length - decimalLength);
            }
        }

         //Start re-joining the string
        newLine = newLine + CurrentColumn + "\t";
    }
于 2013-06-19T07:36:07.680 回答