Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型错误:';新线';此函数的关键字参数无效_Python_Python 3.x - Fatal编程技术网

Python 类型错误:';新线';此函数的关键字参数无效

Python 类型错误:';新线';此函数的关键字参数无效,python,python-3.x,Python,Python 3.x,我编写了以下代码来提取信息。并根据其第二列对象按字母顺序排列: import csv import operator import sys def re_sort(in_file='books.csv', out_file='books_sort.csv'): data = csv.reader(open('books.csv', newline=''), delimiter=',') header = next(data) sortedlist = sorted(da

我编写了以下代码来提取信息。并根据其第二列对象按字母顺序排列:

import csv
import operator
import sys

def re_sort(in_file='books.csv', out_file='books_sort.csv'):
    data = csv.reader(open('books.csv', newline=''), delimiter=',')
    header = next(data)
    sortedlist = sorted(data, key=operator.itemgetter(1))
    with open("books_sorted.csv", "w", newline='') as csvfile:
        cvsWriter = csv.writer(csvfile, delimiter=',')
        cvsWriter.writerow(header)
        cvsWriter.writerows(sortedlist)
每当我尝试在命令行上运行这段代码时,它都会给我错误类型错误:“newline”是此函数的无效关键字参数。你们知道为什么会这样吗。如果文件中的内容是结构化版本,请执行以下操作:

Title,            Author,        Publisher,  Year,  ISBN-10,   ISBN-13
Automate the...,  Al Sweigart,   No Sta...,  2015,  15932...,  978-15932...
Dive into Py...,  Mark Pilgr..., Apress,     2009,  14302...,  978-14302...
"Python Cook...,  "David Bea..., O'Reil...,  2013,  14493...,  978-14493...
Think Python...,  Allen B. D..., O'Reil...,  2015,  14919...,  978-14919...
"Fluent Pyth...,  Luciano Ra..., O'Reil...,  2015,  14919...,  978-14919...

这也许能帮到你。这似乎是一个无效的变量名,用于您尝试执行的操作


open
内置函数在Python3中获得了
newline
关键字,曾经说过,我可以假设您正在使用Python2运行脚本

为了解决您的问题:

  • 确保至少有python v3.2()
  • 并使用正确的python版本运行程序,例如
    python3myscript.py

  • 这可能会对您有所帮助:请参阅关于堆栈溢出的类似文章。将换行符='\n'替换为换行符='\n'这实际上是一个仅链接的答案。请总结一下这个链接的内容。这个链接包含了我问题的解决方案。Ny将“w”改为“wb”,我完全摆脱了新词。一个让提问者满意的答案如何在没有解释的情况下获得否决票?如果这个问题解决了问题,这个问题应该作为另一个问题的副本关闭,而不是作为指向另一个问题的答案关闭