以下查询是抓取数据并创建CSV文件,我遇到的问题是名为“SPLE”的源将数据存储在数据库中,数字为0,1,50。
然而,在CSV中,这些数字正在CSV中收集,我想在某种程度上创建CSV时,这些数字代表单词,例如,
0 =真
1 =假
50 =等待
有人能告诉我这是怎么做的,我一直在努力吗? 我的代码:
从datetime导入日期时间 来自elasticsearch import Elasticsearch import csv
es = Elasticsearch(["9200"])
res = es.search(index="search", body=
{
"_source": ["VT","NCR","N","DT","RD"],
"query": {
"bool": {
"must": [{"range": {"VT": {
"gte": "now/d",
"lte": "now+1d/d"}}},
{"wildcard": {"user": "mike*"}}]}}},size=10)
csv_file = 'File_' + str(datetime.now().strftime('%Y_%m_%d - %H.%M.%S')) + '.csv'
header_names = { 'VT': 'Date', 'NCR': ‘ExTime', 'N': 'Name', 'DT': 'Party', ' RD ': 'Period'}
with open(csv_file, 'w', newline='') as f:
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,)
header_present = True
w.writerow(my_dict)
w.writerow(my_dict)
CSV文件中的输出为:
Date RD Venue
20171016 1 Central
20171016 1 Central
20171016 0 Central
20171016 0 Central
20171016 50 Central
20171016 0 Central
20171016 1 Central
答案 0 :(得分:1)
看起来你让它变得有点复杂,熊猫是你的朋友。
import pandas as pd
def SPLE_fix(sple):
if sple == 0:
return('True')
elif sple == 1:
return('False')
else:
return('Pending')
df=pd.read_csv('mycsvfile.csv')
df['SPLE'] = df['SPLE'].apply(SPLE_fix)
df.to_csv('newcsv.csv', index=False)
newcsv.csv的输出:
Date,SPLE,Venue
20171016,False,Central
20171016,False,Central
20171016,True,Central
20171016,True,Central
20171016,Pending,Central
20171016,True,Central
20171016,False,Central
修改强>
对于大熊猫的解决方案:
import csv
def SPLE_fix(sple):
#just check for text in header
try:
sple[1]=int(sple[1])
except:
return(sple)
#this part just changes the value
if sple[1] == 0:
sple[1] = 'True'
elif sple[1] == 1:
sple[1] = 'False'
else:
sple[1] = 'Pending'
return(sple)
with open('mycsvfile.csv', 'r') as csvfile:
data=csv.reader(csvfile, delimiter=',')
new_data=[SPLE_fix(row) for row in data]
with open('newcsv.csv', 'w', newline='') as csvfile:
cwrite=csv.writer(csvfile, delimiter=',')
cwrite.writerows(new_data)
这应该得到相同的结果。可能不是最有效的,但除非你这么做很多次,否则它不应该太重要。