创建 CSV 文件时删除空白行 - Python
Get rid of blank rows when my CVS file is being created - Python
当我 运行 以下代码时,它会创建 CSV。在我的 CSV 文件中有空白行。我想知道如何去掉空白行。
例如
第 1 行 - 将包含我需要的数据
第 2 行 – 将为空白
第 3 行 - 将包含我需要的数据
第 4 行 – 将为空白
第 5 行 - 将包含我需要的数据
等等
我想要 CSV 文件创建后显示
第 1 行 - 将包含我需要的数据
第 2 行将包含我需要的数据
第 3 行 - 将包含我需要的数据
等等
我的代码如下
from elasticsearch import Elasticsearch
import csv
es = Elasticsearch(["9200"])
# Replace the following Query with your own Elastic Search Query
res = es.search(index="search", body=
{
"_source": ["DTDT", "TRDT", "SPLE", "RPLE"],
"query": {
"bool": {
"should": [
{"wildcard": {"CN": "TEST1"}}
]
}
}
}, size=10)
header_names = { 'DTDT': 'DATE', 'TRDT': 'TIME', ...}
with open('mycsvfile.csv', 'w') as f: # Just use 'w' mode in 3.x
header_present = False
for doc in res['hits']['hits']:
my_dict = doc['_source']
if not header_present:
w = csv.DictWriter(f, my_dict.keys())
w.writerow(header_names) # will write DATE, TIME, ... in correct place
header_present = True
w.writerow(my_dict)
谁能告诉我如何去这个地方。一整天都卡在这个上面。
谢谢
尝试将 newline=''
参数与 writerow()
一起使用。
即w.writerow(my_dict, newline='')
试试这个:
with open('mycsvfile.csv', 'w', newline='') as f:
这是doc
正如文档在 footnote 中所说:
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.
希望对您有所帮助!
我并不是说这是执行此操作的最佳方法,因为我通常自己手动处理 CSV 内容,但是如果您需要快速而肮脏的修复,请尝试在您的代码之后直接执行此操作。
with open('mycsvfile.csv', 'r+') as f:
data = f.read().replace("\n\n", "\n");
f.seek(0);
f.write(data);
这会获取生成的 CSV,将其读入,将任何双换行替换为单换行,然后将其全部写回。远程效率不高,但现在可以使用。
当我 运行 以下代码时,它会创建 CSV。在我的 CSV 文件中有空白行。我想知道如何去掉空白行。
例如
第 1 行 - 将包含我需要的数据
第 2 行 – 将为空白
第 3 行 - 将包含我需要的数据
第 4 行 – 将为空白
第 5 行 - 将包含我需要的数据
等等
我想要 CSV 文件创建后显示
第 1 行 - 将包含我需要的数据
第 2 行将包含我需要的数据
第 3 行 - 将包含我需要的数据
等等
我的代码如下
from elasticsearch import Elasticsearch
import csv
es = Elasticsearch(["9200"])
# Replace the following Query with your own Elastic Search Query
res = es.search(index="search", body=
{
"_source": ["DTDT", "TRDT", "SPLE", "RPLE"],
"query": {
"bool": {
"should": [
{"wildcard": {"CN": "TEST1"}}
]
}
}
}, size=10)
header_names = { 'DTDT': 'DATE', 'TRDT': 'TIME', ...}
with open('mycsvfile.csv', 'w') as f: # Just use 'w' mode in 3.x
header_present = False
for doc in res['hits']['hits']:
my_dict = doc['_source']
if not header_present:
w = csv.DictWriter(f, my_dict.keys())
w.writerow(header_names) # will write DATE, TIME, ... in correct place
header_present = True
w.writerow(my_dict)
谁能告诉我如何去这个地方。一整天都卡在这个上面。
谢谢
尝试将 newline=''
参数与 writerow()
一起使用。
即w.writerow(my_dict, newline='')
试试这个:
with open('mycsvfile.csv', 'w', newline='') as f:
这是doc
正如文档在 footnote 中所说:
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.
希望对您有所帮助!
我并不是说这是执行此操作的最佳方法,因为我通常自己手动处理 CSV 内容,但是如果您需要快速而肮脏的修复,请尝试在您的代码之后直接执行此操作。
with open('mycsvfile.csv', 'r+') as f:
data = f.read().replace("\n\n", "\n");
f.seek(0);
f.write(data);
这会获取生成的 CSV,将其读入,将任何双换行替换为单换行,然后将其全部写回。远程效率不高,但现在可以使用。