简体   繁体   English

尝试从 CSV 文件中获取信息,重新排列列,然后将新输出写入 Python 中的新 CSV 文件

[英]Trying to take info from a CSV file, rearrange the columns and then write the new output to a new CSV file in Python

I have a set of data in a CSV file that basically requires re-ordering and the re-ordered data writing to a new CSV file.我在 CSV 文件中有一组数据,基本上需要重新排序并将重新排序的数据写入新的 CSV 文件。 The data looks like this to start数据看起来像这样开始

Communit,Equtions,8000,707757,2024.96,0,99
Annlins,EXSES,5063,536835,71.26,0,99
K ad,EXPSES,3028,40360,37.31,0,99
Harr White,EXSES,1644,10634264,85.55,0,99
Emge,Equutions,89250,68895,93.53,0,99
HMC,120PE249,83210,12039,1651.86,0,99

7 columns of data separated by a comma. 7 列数据,以逗号分隔。 To make it a bit more readable I shall focus on the first line.为了使其更具可读性,我将重点关注第一行。

So it starts like - Communit,Equtions,8000,707757,2024.96,0,99 And needs to end up like - Communit,8000,707757,2024.96,Equtions,99所以它开始像 - Communit,Equtions,8000,707757,2024.96,0,99最终需要像 - Communit,8000,707757,2024.96,Equtions,99

My current code can print it to the screen but I'm struggling to get it to write to a file我当前的代码可以将其打印到屏幕上,但我正在努力让它写入文件

import csv

with open('C:\\Impexp\\Input\\02B-210722.csv') as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        print(",".join([row[0], row[2], row[3], row[4], row[1], row[6]]))

I did try changing the sys.stdout to a file but that wouldn't work.我确实尝试将sys.stdout更改为文件,但这不起作用。

I'm a bit rusty with my coding as I mainly use SQL and spreadsheets are my primary focus and this is my first time dabbling with Python.我的编码有点生疏,因为我主要使用 SQL,而电子表格是我的主要关注点,这是我第一次涉足 Python。

Any help appreciated, have tried looking at other posts to try and cobble together a solution that fits my problem, but so far no joy.任何帮助表示赞赏,已尝试查看其他帖子以尝试拼凑出适合我的问题的解决方案,但到目前为止没有任何乐趣。

You can use pandas to rearrange the columns:您可以使用 pandas 重新排列列:

import pandas as pd

df = pd.read_csv('data.csv')
new_cols = ['A', 'C', 'D', 'E', 'F', 'B', 'G']
df = df[new_cols]
df.to_csv('new_data.csv', index=False)

data.csv数据.csv

A,B,C,D,E,F,G
Communit,Equtions,8000,707757,2024.96,0,99
Annlins,EXSES,5063,536835,71.26,0,99
K ad,EXPSES,3028,40360,37.31,0,99
Harr White,EXSES,1644,10634264,85.55,0,99
Emge,Equutions,89250,68895,93.53,0,99
HMC,120PE249,83210,12039,1651.86,0,99

new_data.csv新数据.csv

A,C,D,E,F,B,G
Communit,8000,707757,2024.96,0,Equtions,99
Annlins,5063,536835,71.26,0,EXSES,99
K ad,3028,40360,37.31,0,EXPSES,99
Harr White,1644,10634264,85.55,0,EXSES,99
Emge,89250,68895,93.53,0,Equutions,99
HMC,83210,12039,1651.86,0,120PE249,99

Or, using the csv module:或者,使用 csv 模块:

import csv

with open('data.csv') as f, open('new_data.csv', 'w', newline='') as g:
    reader = csv.reader(f, delimiter=',')
    writer = csv.writer(g, delimiter=',')
    for row in reader:
        writer.writerow([row[0], row[2], row[3], row[4], row[1], row[6]])

data.csv数据.csv

Communit,Equtions,8000,707757,2024.96,0,99
Annlins,EXSES,5063,536835,71.26,0,99
K ad,EXPSES,3028,40360,37.31,0,99
Harr White,EXSES,1644,10634264,85.55,0,99
Emge,Equutions,89250,68895,93.53,0,99
HMC,120PE249,83210,12039,1651.86,0,99

new_data.csv新数据.csv

Communit,8000,707757,2024.96,Equtions,99
Annlins,5063,536835,71.26,EXSES,99
K ad,3028,40360,37.31,EXPSES,99
Harr White,1644,10634264,85.55,EXSES,99
Emge,89250,68895,93.53,Equutions,99
HMC,83210,12039,1651.86,120PE249,99

You can use csv.writer() to write patched data into another CSV:您可以使用csv.writer()将修补后的数据写入另一个 CSV:

import csv

with open(r'C:\Impexp\Input\02B-210722.csv') as i_f, \
        open(r'C:\Impexp\Input\02B-210722_patched.csv', 'w', newline='') as o_f:
    reader = csv.reader(i_f)
    writer = csv.writer(o_f)
    for row in reader:
        row[4] = row.pop(1)
        writer.writerow(row)

If you want to modify existing file without creating new (which I don't recommend you to do without backups) you can open file in r+ mode, read all rows into list and rewrite same file:如果您想修改现有文件而不创建新文件(我不建议您在没有备份的情况下这样做) ,您可以在r+模式下打开文件,将所有行读入列表并重写同一文件:

import csv

with open(r'C:\Impexp\Input\02B-210722.csv', 'r+', newline='') as f:
    reader = csv.reader(f)
    rows = []
    for row in reader:
        row[4] = row.pop(1)
        rows.append(row)
    f.seek(0)
    writer = csv.writer(f)
    writer.writerows(rows)
    f.truncate()

You can help my country, check my profile info .你可以帮助我的国家,查看我的个人资料信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM