0

Does anyone have any idea or know of any situation where python inserts newline characters strangely.

This is my piece of code

  if ((sentAnalyze) and len(OpString)!=0):
    if data[8]!= '':
        if (data[8] == 'p'):
            OpString = "1 " + OpString
        elif (data[8] == 'n'):
            OpString = "-1 " + OpString
        elif (data[8] == 'neu'):
            OpString = "0 " + OpString
        print "writing :", OpString
        fw.write(OpString + "\n")

When I try looking at the print commands and the file write commands, there is a an extra new line inserted in the file for certain line numbers.

this entire if block is in a while loop and the print command prints the lines properly.

and yeah I am opening the file in w+ mode.

Function (partially written here) to compute the OpString

 for word,tag in simplified_tokens:
                tok = word + "/" + tag
                if tok in self.wordtoPosition:
                    OpString = OpString+ " " + str(self.wordtoPosition[tok]) + ":1" #+ str(1.0 / self.uniqPOSHash[tok])
return OpString

And the data looks like

1981:1 503:1 21:1 58:1 159:1 1:1 87:1 412:1 105:1 478:1 1154:1 1023:1 1192:1 53:1 37:1 36:1 598:1 19:1 4:1 162:1 14:1 131:1 2:1 489:1 411:1 3:1 165:1 370:1


-17:1 614:1 6:1 631:1 59:1 1:1 0:1 1183:1 10:1 22:1 15:1 67:1 55:1 3:1 175:1 9:1 43:1 866:1 48:1 30:1 0:1 484:1 2:1 1106:1 109:1

Notice the extra newline between. Only happening at certain places.

repr(OpString) shows

'1981:1 503:1 21:1 58:1 159:1 1:1 87:1 412:1 105:1 478:1 1154:1 1023:1 1192:1 53:1 37:1 36:1 598:1 19:1 4:1 162:1 14:1 131:1 2:1 489:1 411:1 3:1 165:1 370:1'
'-17:1 614:1 6:1 631:1 59:1 1:1 0:1 1183:1 10:1 22:1 15:1 67:1 55:1 3:1 175:1 9:1 43:1 866:1 48:1 30:1 0:1 484:1 2:1 1106:1 109:1'

Note. Removing "\n" will still add a line when I am writing to the file for certain lines. That is weird

Another Interesting Observation. In the above approach, I am reading from a file and writing into a different file. sO i have 2 file handles open. fr for file read and fw for file write. If I just have fr open, build the entire OpString variable, close fr and then write it using fw, I do not get weird new lines. Very interesting!

4

1 回答 1

3

我们真的必须看看你正在使用的输入数据..与此同时,你能试试吗

fw.write(Opstring.strip() + '\n')

strip()在为write.

我怀疑至少你的一些OpStrings 已经有一个尾随换行符。

这不是一种修复,而是一种诊断。一旦确定这是问题的根源,那么更好的方法是在原点/阅读时消除差事换行符。

更新

更好的是,做一个

 Opstring = Opstring.strip()

在您if根据@kindall 的有用评论输入 -statement之前

于 2012-07-02T21:35:14.930 回答