重新排列CSV列

时间:2017-10-18 15:12:01

标签: python python-3.x csv python-3.6

创建CSV文件时,列不在我想要的位置。例如,列'' Period'(此变量为'RD')是文件中的第二列等等。

有没有办法将每列的位置设置到我想要的位置?

我的代码:

from datetime import datetime
from 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)

3 个答案:

答案 0 :(得分:3)

使用pandas非常简单:

import pandas as pd


# Read csv / tab-delimited in this example
df = pd.read_csv('example.csv', sep='\t')

print df

   A  B  C
0  4  5  9
1  4  5  9
2  4  5  9
3  4  5  9

# Reorder columns
df = df[['C', 'A', 'B']]

print df

   C  A  B
0  9  4  5
1  9  4  5
2  9  4  5
3  9  4  5

# Write csv / tab-delimited
df.to_csv('example.csv', sep='\t')

答案 1 :(得分:2)

字典未订购,如果要强制执行列排序,则需要明确指定

import csv
headers = ['Party', 'Period', 'Date', 'ExTime', 'Name'] # Don't use my_dict.keys()
with open('header.csv', 'w') as f:
    w = csv.DictWriter(f, fieldnames=headers)
    w.writeheader()

$ python sample.py && cat header.csv
Party,Period,Date,ExTime,Name

当您拨打w.writerow(my_dict)时,字典将根据标题进行排序。

row = {'Period':2, 'Date':3, 'Name':5, 'Party': 1, 'ExTime':4}
w.writerow(row)

输出

Party,Period,Date,ExTime,Name
1,2,3,4,5

答案 2 :(得分:1)

当您处理csv文件时,最好为您的应用程序使用pandas。

import pandas as pd

# Let your file have 4 columns named c1, c2, c3 and c4
# And assume you want to reorder it to c2, c3, c1, c4

data_frame = pd.read_csv('filename.csv', delimiter=',') # reading csv file as data frame with pandas

new_data_frame = data_frame[['c2', 'c3', 'c1', 'c4']] # reordered the dataframe and stored in new_data_frame

# If you want to save the result to new csv file

new_data_frame.to_csv('altered.csv', index=None)

在您的情况下,假设列和分隔符的顺序是','

import pandas as pd

csv_file_name = 'File_' + str(datetime.now().strftime('%Y_%m_%d - %H.%M.%S')) + '.csv'

data_frame = pd.read_csv(csv_file_name, delimiter=',') # change delimiter to '\t' if needed

new_data_frame = data_frame[['Party', 'Period', 'Date', 'ExTime', 'Name']]

new_data_frame.to_csv('filename.csv', index=None)