Python unidecode生成额外的\n
我使用下面的代码删除字符串中的重音符号。 输入(inp)是一个带有带重音符号的字符串的文件。 输出(outp)是一个文件 代码如下:Python unidecode生成额外的\n,python,string,unicode,line-breaks,Python,String,Unicode,Line Breaks,我使用下面的代码删除字符串中的重音符号。 输入(inp)是一个带有带重音符号的字符串的文件。 输出(outp)是一个文件 代码如下: 49 def remove_unidecode(inp, outp): 50 ''' Remove accent and special characters 51 + lower case ''' 52 print "# remove accents" 53 for line in inp: 54 uline =
49 def remove_unidecode(inp, outp):
50 ''' Remove accent and special characters
51 + lower case '''
52 print "# remove accents"
53 for line in inp:
54 uline = unicode(line, 'utf-8')
55 udline = unidecode(uline)
56 outp.write(udline.lower())
57 outp.close()
问题是unidecode正在创建额外的\n,即打印额外的换行符:
$ wc -l *
3619 inp.txt
3879 outp.txt
有人知道为什么会发生这种情况吗?我将第56行更改为:outp.write(udline.rstrip('\n').lower()),但错误仍然存在。我想我们需要查看
unidecode
来告诉您这里出了什么问题。文件是如何打开的?如果以二进制模式打开输入文件并以文本模式输出,则如果输入有\r\n行结尾,则换行符将加倍。它们将更改为\r\n\n。这里是unidecode:文件是这样打开的:inp=open(path\u inp,'r')
和outp=open(path\u out,'w')
。