Python readline 忽略 csv 文件中的 header
Python readline ignoring header in csv file
我写了一些代码,应该知道 CSV 文件是否存在,如果格式正确则附加到它,或者删除它并创建一个新文件。
检查格式是否正确的部分(通过检查 CSV headers 看它们是否相等)不工作,因为某些奇怪的原因 readline()
函数忽略了headers(应该是 CSV 文件的第一行。
请注意,我不想使用额外的依赖项,例如 Pandas。
import os, csv
path = 'intent_outputs.csv'
response = {'query':'eggs', 'header':'eggs', 'fulfillmentText':'are tasty', 'buttontext':'VISIT PAGE', 'buttonurl':'eggs.com', 'contextbuttontext':'eggs', 'contextbuttonurl':'eggs.com'}
output = open(path, 'a')
csv_columns = ['Query', 'Header', 'Text', 'Button Text', 'Button URL', 'Context Button Text', 'Context Button URL']
writer = csv.DictWriter(output, fieldnames=csv_columns)
if not os.path.exists(path):
print("DOES NOT EXIST")
output = open(path, 'w')
else:
print("EXISTS")
output = open(path, 'r')
if(output.readline() != ",".join(csv_columns)):
print("NOT EQUAL")
try:
output = open(path, 'w')
writer.writeheader()
except IOError:
print("IOError")
else:
print("EQUAL")
output = open(path, 'a')
try:
row = {'Query':response['query'], 'Header':response['header'], 'Text':response['fulfillmentText'], 'Button Text':response['buttontext'], 'Button URL':response['buttonurl'], 'Context Button Text':response['contextbuttontext'], 'Context Button URL':response['contextbuttonurl']}
writer.writerow(row)
except IOError:
print("I/O error")
这里是 Johnny intent_outputs.csv
:
Query,Header,Text,Button Text,Button URL,Context Button Text,Context Button URL
eggs,eggs,are tasty,VISIT PAGE,eggs.com,eggs,eggs.com
我想阅读第一行(从 "Query...." 开始),但它被主动忽略了。
readline()
输出在末尾包含换行符 \n
。
所以output.readline()
returns
Query,Header,Text,Button Text,Button URL,Context Button Text,Context Button URL\n
也许这就是为什么出现这种情况 if(output.readline() != ",".join(csv_columns)):
returns True
.
最后我自己解决了这个问题。所以我重写了函数,使其不依赖于打开的文件,除了上面代码中的一些额外逻辑错误外,下面的脚本可以完美地按预期工作。
@Suitsense 的回答让我知道了readline()
最后加的\n
,谢谢!
path = 'intent_outputs.csv'
response = {'query':'eggs', 'header':'eggs', 'fulfillmentText':'are tasty', 'buttontext':'VISIT PAGE', 'buttonurl':'eggs.com', 'contextbuttontext':'eggs', 'contextbuttonurl':'eggs.com'}
csv_columns = ['Query', 'Header', 'Text', 'Button Text', 'Button URL', 'Context Button Text', 'Context Button URL']
output = ""
writer = ""
try:
output = open(path,'x')
except FileExistsError:
output = open(path,'r')
if(open(path,'r').readline() != ",".join(csv_columns)+"\n"):
try:
output = open(path, 'w')
writer = csv.DictWriter(output, fieldnames=csv_columns)
writer.writeheader()
except IOError:
print("IOError: Couldn't write header")
else:
output = open(path, 'a')
writer = csv.DictWriter(output, fieldnames=csv_columns)
row = {'Query':response['query'], 'Header':response['header'], 'Text':response['fulfillmentText'], 'Button Text':response['buttontext'], 'Button URL':response['buttonurl'], 'Context Button Text':response['contextbuttontext'], 'Context Button URL':response['contextbuttonurl']}
writer.writerow(row)
我写了一些代码,应该知道 CSV 文件是否存在,如果格式正确则附加到它,或者删除它并创建一个新文件。
检查格式是否正确的部分(通过检查 CSV headers 看它们是否相等)不工作,因为某些奇怪的原因 readline()
函数忽略了headers(应该是 CSV 文件的第一行。
请注意,我不想使用额外的依赖项,例如 Pandas。
import os, csv
path = 'intent_outputs.csv'
response = {'query':'eggs', 'header':'eggs', 'fulfillmentText':'are tasty', 'buttontext':'VISIT PAGE', 'buttonurl':'eggs.com', 'contextbuttontext':'eggs', 'contextbuttonurl':'eggs.com'}
output = open(path, 'a')
csv_columns = ['Query', 'Header', 'Text', 'Button Text', 'Button URL', 'Context Button Text', 'Context Button URL']
writer = csv.DictWriter(output, fieldnames=csv_columns)
if not os.path.exists(path):
print("DOES NOT EXIST")
output = open(path, 'w')
else:
print("EXISTS")
output = open(path, 'r')
if(output.readline() != ",".join(csv_columns)):
print("NOT EQUAL")
try:
output = open(path, 'w')
writer.writeheader()
except IOError:
print("IOError")
else:
print("EQUAL")
output = open(path, 'a')
try:
row = {'Query':response['query'], 'Header':response['header'], 'Text':response['fulfillmentText'], 'Button Text':response['buttontext'], 'Button URL':response['buttonurl'], 'Context Button Text':response['contextbuttontext'], 'Context Button URL':response['contextbuttonurl']}
writer.writerow(row)
except IOError:
print("I/O error")
这里是 Johnny intent_outputs.csv
:
Query,Header,Text,Button Text,Button URL,Context Button Text,Context Button URL
eggs,eggs,are tasty,VISIT PAGE,eggs.com,eggs,eggs.com
我想阅读第一行(从 "Query...." 开始),但它被主动忽略了。
readline()
输出在末尾包含换行符 \n
。
所以output.readline()
returns
Query,Header,Text,Button Text,Button URL,Context Button Text,Context Button URL\n
也许这就是为什么出现这种情况 if(output.readline() != ",".join(csv_columns)):
returns True
.
最后我自己解决了这个问题。所以我重写了函数,使其不依赖于打开的文件,除了上面代码中的一些额外逻辑错误外,下面的脚本可以完美地按预期工作。
@Suitsense 的回答让我知道了readline()
最后加的\n
,谢谢!
path = 'intent_outputs.csv'
response = {'query':'eggs', 'header':'eggs', 'fulfillmentText':'are tasty', 'buttontext':'VISIT PAGE', 'buttonurl':'eggs.com', 'contextbuttontext':'eggs', 'contextbuttonurl':'eggs.com'}
csv_columns = ['Query', 'Header', 'Text', 'Button Text', 'Button URL', 'Context Button Text', 'Context Button URL']
output = ""
writer = ""
try:
output = open(path,'x')
except FileExistsError:
output = open(path,'r')
if(open(path,'r').readline() != ",".join(csv_columns)+"\n"):
try:
output = open(path, 'w')
writer = csv.DictWriter(output, fieldnames=csv_columns)
writer.writeheader()
except IOError:
print("IOError: Couldn't write header")
else:
output = open(path, 'a')
writer = csv.DictWriter(output, fieldnames=csv_columns)
row = {'Query':response['query'], 'Header':response['header'], 'Text':response['fulfillmentText'], 'Button Text':response['buttontext'], 'Button URL':response['buttonurl'], 'Context Button Text':response['contextbuttontext'], 'Context Button URL':response['contextbuttonurl']}
writer.writerow(row)