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

8e2ybdfx  于 2022-12-25  发布在  Python
关注(0)|答案(2)|浏览(166)

我在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

用逗号分隔的7列数据。为了使它更容易阅读,我将重点放在第一行。
因此,它以-Communit,Equtions,8000,707757,2024.96,0,99开头,需要以-Communit,8000,707757,2024.96,Equtions,99结尾
我目前的代码可以将其打印到屏幕上,但我很难将其写入文件

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]]))

我确实尝试过将sys.stdout更改为文件,但那不起作用。
我对我的编码有点生疏,因为我主要使用SQL和电子表格是我的主要关注点,这是我第一次涉足Python。
任何帮助感谢,已尝试寻找其他职位,试图拼凑一个解决方案,适合我的问题,但到目前为止没有喜悦。

6ojccjat

6ojccjat1#

您可以使用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)

如果你想修改现有的文件而不创建新的 (我不建议你这样做没有备份) 你可以打开文件在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()
zfycwa2u

zfycwa2u2#

您可以使用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

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

新数据.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

或者,使用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

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

新数据.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

相关问题