Python - 输出创建空格

时间:2011-11-28 19:32:34

标签: python

我在编写的python脚本中遇到了一个非常奇怪的问题。这是产生异常结果的代码段:

编辑:我现在已将整个循环包含在代码段中。

data = open(datafile,'r')
outERROR = open(outERRORfile,'w')
precision=[0]
scale=[0]
lines = data.readlines()
limit = 0
if filetype == 'd':
    for line in lines:
            limit += 1
            if limit > checklimit:
                    break
            columns = line.split(fieldDelimiter)
            for i in range(len(columns) - len(precision)):
                            precision.append(0)
            for i in range(len(columns) - len(scale)):
                            scale.append(0)
            if len(datatype) != len(precision):
                    sys.exit() #Exits the script if the number of data types (fields found in the DDL file) doesn't match the number of columns found in the data file
            i = -1
            for eachcolumn in columns:
                    i += 1
                    if len(rstrip(columns[i])) > precision[i]:
                            precision[i] = len(rstrip(columns[i]))
                    if columns[i].find('.') != -1 and (len(rstrip(columns[i])) - rstrip(columns[i]).find('.')) > scale[i]:
                            scale[i] = len(rstrip(columns[i])) - rstrip(columns[i]).find('.') -1
                    if datatype[i][0:7] == 'integer':
                            if int(columns[i]) < -2147483648 or int(columns[i]) > 2147483647:
                                    outERROR.write("Integer value too high or too low to fit inside Integer data type, column: " + str(i + 1) + ", value: " + columns[i] + "\n")
                    if datatype[i][0:9] == 'smallint':
                            if int(columns[i]) < -32768 or int(columns[i]) > 32767:
                                    outERROR.write("Smallint value too high or too low to fit inside Smallint data type, column: " + str(i + 1) + ", value: " + columns[i] + "\n")
                    if datatype[i][0:7] == 'byteint':
                            if int(columns[i]) < -128 or int(columns[i]) > 127:
                                    outERROR.write("Byteint value too high or too low to fit inside Byteint data type, column: " + str(i + 1) + ", value: " + columns[i] + "\n")
                    if datatype[i][0:4] == 'date':
                            if DateParse(columns[i],format1[i]) > -1:
                                    pass
                            elif DateParse(columns[i],format2[i]) > -1:
                                    pass
                            elif DateParse(columns[i],format3[i]) > -1:
                                    pass
                            else:
                                    outERROR.write('Date format error, column: ' + str(i + 1) + ', value: ' + columns[i])
                    if datatype[i][0:9] == 'timestamp':
                            if DateParse(columns[i],timestamp1[i]) > -1:
                                    pass
                            elif DateParse(columns[i],timestamp2[i]) > -1:
                                    pass
                            elif DateParse(columns[i],timestamp3[i]) > -1:
                                    pass
                            else:
                                    outERROR.write('Timestamp format error, column: ' + str(i + 1) + ', value: ' + columns[i] + '\n')
                    if (datatype[i][0:7] == 'decimal'
                    or datatype[i][0:7] == 'integer'
                    or datatype[i][0:7] == 'byteint'
                    or datatype[i][0:5] == 'float'
                    or datatype[i][0:8] == 'smallint'):
                            try:
                                    y = float(columns[i])
                            except ValueError:
                                    outERROR.write('Character found in numeric data type, column: ' + str(i + 1) + ', value: ' + columns[i] + "\n")
                    else:
                            pass

这是读取数据文件的循环的一部分,基本上是检查数据的类型(以确定它是否应该是数字类型)并'尝试'将其转换为浮点数以查看它是否实际数据文件中的数字数据。如果它不是数字数据,它会将您在上面看到的错误输出到文本文件(当前定义为outERROR)。现在,当我写这个并在一个小数据文件(4行)上测试它时工作正常,但是当我在一个更大的文件(几千行)上运行时,我的错误文件突然填满了一堆空格,并且只有正在创建一些错误消息。

以下是运行4行脚本时错误文件的样子:

Character found in numeric data type, column: 6, value: 24710a35
Character found in numeric data type, column: 7, value: 0a04
Character found in numeric data type, column: 8, value: 0a02
Character found in numeric data type, column: 6, value: 56688a12
Character found in numeric data type, column: 7, value: 0a09
Character found in numeric data type, column: 8, value: 0a06
Character found in numeric data type, column: 6, value: 12301a04
Character found in numeric data type, column: 7, value: 0a10
Character found in numeric data type, column: 8, value: 0a02
Character found in numeric data type, column: 6, value: 25816a56
Character found in numeric data type, column: 7, value: 0a09
Character found in numeric data type, column: 8, value: 0a06

这是预期的输出。

当我在较大的文件上运行它时,我开始在错误文件的顶部获得空格,并且只有最后40-50左右的错误写入实际上在文件中作为文本输出。文件越大,输出的空格越多。我完全迷失了,我已经在stackoverflow.com上阅读了一些关于神秘空白行和空格的其他问题,但他们似乎没有解决我的问题。

编辑:outERROR是我给输出写入的错误文件的名称。这是一个简单的.txt文件。

这是数据文件的示例:

257|1463|64|1|7|9551a22|0a05|0a02|N|O|1998-06-18|1998-05-15|1998-06-27|COLLECT COD|FOB|ackages sleep bold realmsa f|
258|1062|68|1|8|7704a48|0a00|0a07|R|F|1994-01-20|1994-03-21|1994-02-09|NONE|REG AIR|ully about the fluffily silent dependencies|
258|1962|95|2|40|74558a40|0a10|0a01|A|F|1994-03-13|1994-02-23|1994-04-05|DELIVER IN PERSON|FOB|silent frets nod daringly busy, bold|
258|1618|19|3|45|68382a45|0a07|0a07|R|F|1994-03-04|1994-02-13|1994-03-30|DELIVER IN PERSON|TRUCK|regular excuses-- fluffily ruthl|

具体而言,导致输出到错误文件的列是:

|9551a22|0a05|0a02|
|7704a48|0a00|0a07|
|74558a40|0a10|0a01|
|68382a45|0a07|0a07|

因此每行应该对错误文件进行3次写入,指定这些值。它适用于少量线条,但当它读取大量线条时,我开始得到这些神秘的空白区域。当我的数字字段包含字符时,会出现此问题

2 个答案:

答案 0 :(得分:1)

猜测,也许您在输入流中有控制字符会导致一些意外行为。不确定outERROR在上面的上下文中究竟是什么,但是你可以想象,例如,输入中的换页符可能会产生这种效果。

首先尝试清除不可打印字符的数据,看看是否有帮助。

答案 1 :(得分:-1)

使用'rb'和'wb'调用open以确保二进制模式,否则系统会在试图弄乱行结尾时更改数据

相关问题