[英]Csv Reader expected a character buffer object
I have DNS record as csv format and it has HostName and IPAdress Label. 我有DNS记录为csv格式,并且它具有主机名和IPAdress标签。 İt's include public and private ("192.168", "172.16" and "10.0")IP address and ı want to separate each other.
它包括公用和专用(“ 192.168”,“ 172.16”和“ 10.0”)IP地址,并且想要彼此分开。 When ı running my code ı took "expected a character buffer object " error code.
当我运行我的代码时,我采取了“预期的字符缓冲区对象”错误代码。 How can solve this issue
如何解决这个问题
import csv
with open('Dns.csv', 'rb') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if "10.205" in row['IPAddress']:
file10=open("Zone10.txt","a")
a=(row['hostname'], row['IPAddress'])
file10.write(a)
The reason is you are trying to write tuple to file10
. 原因是您正在尝试将元组写入
file10
。 So next lines should be modified 所以下一行应该修改
a=(row['hostname'], row['IPAddress'])
file10.write(a)
For example, If you're going to write it as a string you can do: 例如,如果要将其编写为字符串,则可以执行以下操作:
file10.write(' '.join(a)) # it will work
or 要么
a='{}{}'.format(row['hostname'], row['IPAddress'])
file10.write(a)
Btw it also would be better to use context manager to open file10
. 顺便说一句,最好使用上下文管理器打开
file10
。
Regarding to comment about CSV output: 关于对CSV输出的评论:
output = []
for row in reader:
if "10.205" in row['IPAddress']:
output.append(row)
if output:
with open('Zone.csv', 'wb') as zonefile:
writer = csv.DictWriter(zonefile, fieldnames=output[0].keys())
writer.writeheader()
writer.writerows([row for row in output])
It is very quick-written code, but I hope it will help 这是非常快速编写的代码,但希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.