使用来自另一个

我正在尝试创建一个脚本,该脚本从 CSV 文件中选择一些列并将它们保存到另一个列中(最好指定列标题)。 这是我开始的查询,它将复制所有列。如何将其更改为仅复制其中的一部分?

# importing openpyxl module
import openpyxl as xl;
  
# opening the source excel file
filename ="C:\\Users\\...\\input.clv"
wb1 = xl.load_workbook(filename)
ws1 = wb1.worksheets[0]
  
# opening the destination excel file 
filename1 ="C:\\Users\\...\\output.clv"
wb2 = xl.load_workbook(filename1)
ws2 = wb2.active
  
# calculate total number of rows and 
# columns in source excel file
mr = ws1.max_row
mc = ws1.max_column
  
# copying the cell values from source 
# excel file to destination excel file
for i in range (1,mr + 1):
    for j in range (1,mc + 1):
        # reading cell value from source excel file
        c = ws1.cell(row = i,column = j)
  
        # writing the read value to destination excel file
        ws2.cell(row = i,column = j).value = c.value
  
# saving the destination excel file
wb2.save(str(filename1))

先谢谢你!

kukududushen 回答:使用来自另一个

这就是我使用 Python 文件读/写的方式。

def readCsv(fileName):
  data = []
  myFile = open(fileName,"r")
  for line in myFile:
    lineList = line.split(",")
    lineList[len(lineList)-1] = lineList[len(lineList) - 1].replace("\n","")
    data.append(lineList)
  myFile.close()
  return data

def writeCsv(data):
  dataString = ""
  for line in data:
    dataString =dataString + ','.join(line)+"\n"
  myNewFile = open("output.csv","w")
  myNewFile.write(dataString)
  myNewFile.close()

data = readCsv("yourCsv.csv")
# Remove the data you don't need
writeCsv(dataAfterRemovingColumns)

My readCsv 函数生成一个 2D 列表,其中每一项都是 CSV 文件一行中的数据列表。因此,在我评论 # Remove the data you don't need 的地方,您将遍历 2D 列表,从构成您要删除的列的一部分的每一行中删除项目。希望这是有道理的!

,

根据您将 csv 文件中的某些列保存到另一个文件的目的,您可以按如下方式使用 Pandas 库:

import pandas as pd

def save_csv(df,path,cols):
    df[cols].to_csv(path,index=False)

with open('path/to/csv',r) as f:
    df = pd.read_csv(f)

# Assuming you want to save columns colA and colB
save_csv(df,path/to/dest/csv,['colA','colB'])

您也可以使用 csv DictReader、DictWriter 作为另一种代码更长但时间更快的方法(基于我的时间):

import csv

def use_csv():
    def new_dict(d,cols):
        new_dict = {}
        for col in cols:
            new_dict[col] = d[col]
        return new_dict

    with open('path/to/csv','r') as f:
        df = csv.DictReader(f)

        with open('path/to/dest/csv','w') as csvfile:
            fieldnames = ['colA','colB']
            writer = csv.DictWriter(csvfile,fieldnames=fieldnames)

            writer.writeheader()
            for row in df:
                data = new_dict(row,fieldnames)
                writer.writerow(data)
,

您可以使用 stdlib 中的 CSV

#!/usr/bin/env python

import csv

inputCsvFilePath  = 'input.csv'
outputCsvFilePath = 'output.csv'

inputCsvColumnNumbers  = [1,3,5]
outputCsvColumnHeaders = ['one','three','five']

# reading/writing row by row (high IO,low memory):
with open(inputCsvFilePath) as inputCsv:
    inputCsvReader = csv.reader(inputCsv)

    with open(outputCsvFilePath,'w') as outputCsv:
        outputCsvWriter = csv.writer(outputCsv)

        # write custom csv header:
        outputCsvWriter.writerow(outputCsvColumnHeaders)
        # skip input file header:
        inputCsvReader.__next__()

        for inputRow in inputCsvReader:
            outputCsvWriter.writerow( [inputRow[i] for i in inputCsvColumnNumbers] )

就我个人而言,我会为此使用 sqlite:

#!/bin/bash

sqlite3 <<EOF
-- input:
.separator ',' "\n"

.import 'input.csv' inputData


-- output:
.mode csv
.header on
.once 'output.csv'

select
  user_id  as "one",login_id as "three",password as "five"
from inputData
;
EOF
本文链接:https://www.f2er.com/10855.html

大家都在问