python排序函数在windows上似乎不起作用

2024-09-30 00:26:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python3对csv文件进行排序,代码如下。它在MAC上运行得很好,但在Windows(real或Fusion)上我遇到了这个例外。有人看到这个或者有什么建议吗?在

Exception in Tkinter callback Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
    return self.func(*args)
File "Z:\bin\FDTSEfiles\HrsWonVal.py", line 470, in <lambda>
    Button(newOrExisting, text="OK", command=lambda: getnewOrExisting_File(U_cmd.get())).grid(row=5, column=1)
File "Z:\bin\FDTSEfiles\HrsWonVal.py", line 466, in getnewOrExisting_File
    reverseOrderAndFilterFile(existingFilePath)
File "Z:\bin\FDTSEfiles\HrsWonVal.py", line 510, in reverseOrderAndFilterFile
    sortedlist = sorted(data, key=operator.itemgetter(3))

# 3 specifies the 4th column - Date IndexError: list index out of range

我的代码:

^{pr2}$

Tags: csvlambda代码inpybinlinecolumn
2条回答

csv模块docs中的脚注:

If newline='' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline='', since the csv module does its own (universal) newline handling.

Windows恰好是一个(…the)平台,它使用\r\n作为行结尾。这就是为什么要获得特定于平台的行为;通过将newline=''指定到open来删除它。在

无论平台是什么,sorted都能正常工作。回溯告诉您错误发生的确切位置,显然这与您的数据不是您认为的那样(或者没有被csv模块解释为)有关-要么定界符错误,要么源文件的列少于四列。在

相关问题 更多 >

    热门问题