import csv
with open('myaddresses.csv','w','newline')as A:
thewriter = cav.writer(A)
thewriter.writerow({'11 Wall Street, New York, NY, 10005'})
thewriter.writerow({'350 Fifth Avenue, New York, NY, 10118'})
thewriter.writerow({'1600 Pennsylvania Avenue NW, Washington DC, 20500'})
thewriter.writerow({'4059 Mt Lee Dr.,Hollywood, CA, 90068'})
thewriter.writerow({'Statue of Liberty, Liberty Island, New York, NY, 10004'})
thewriter.writerow({'1313 Mockingbird Lane, Albany, NY, 12084'})
thewriter.writerow({'0001 Cemetery Lane, Westfield, NJ, 07091'})
thewriter.writerow({'3109 Grant Ave, Philadelphia , Pa, 19114'})
thewriter.writerow({'532 N. 7th Street, Philadelphia, PA, 19123'})
thewriter.writerow({'317 Chestnut Street, Philadelphia, PA, 19106'})
TypeError:"str"对象不能被解释为整数
任何帮助都将涉及
我只需要它来打印我输入的信息。
open不接受作为字符串的参数"newline"。它可以采用关键字参数newline =
。正如文档所说:换行符决定了如何解析流中的换行符。它可以是None、"、"\n"、"\r"one_answers"\r\n"。
问题在于如何调用open()
函数。
# This call will result in TypeError
with open('myaddresses.csv','w','newline')as A:
# do something with file A
参数'newline'
分配不正确。如果我们看一下函数定义:
打开(文件,模式=‘r’,缓冲=-1,编码=无,错误=无,换行=无,关闭fd=真,开启器=无(
当您只使用位置参数调用open()
函数时,您正试图将值'newline'
分配给只接受整数的buffering
。
在您的情况下,调用open()
的正确方法是使用newline
的关键字参数,即:
with open('myaddresses.csv', 'w', newline="n") as A:
# do something with file A
newline
参数只能是以下参数之一:
无、"、"\n"、"\r"one_answers"\r\n"。
newline
参数的默认值为None
。