简体   繁体   English

如何将Excel公式应用于整个列中的值?

[英]How do I apply an Excel formula to the values in an entire column?

I am trying to parse file names in a folder and figured out how to load them into a csv file but I need to apply a formula to the column in order to get my final results. 我试图解析文件夹中的文件名,并弄清楚如何将它们加载到csv文件中,但是我需要对列应用公式才能获得最终结果。 The code and data is as follows along with the Excel formula I plan on using. 代码和数据以及我计划使用的Excel公式如下。

This snippet of code takes the file names from the folder "testfolder" and dumps them in the csv file. 此代码段从文件夹“ testfolder”获取文件名,并将其转储到csv文件中。

Code: 码:

import os, csv

f = open("/home/niteninja/Documents/sampledata.csv", 'r+')
w=csv.writer(f)
for path, dirs, files in os.walk("/home/niteninja/testfolder"):
    for filename in files:
        w.writerow([filename])

f.truncate()

This is an example of what the data in the csv file looks like after running the code above. 这是运行上面的代码后csv文件中数据的外观示例。

sampledata.csv: sampledata.csv:

       A                  B

ndjsmdhfr_145874 

jdmsjfht_273647

hfgryuyw_756475

jgmfhdhr_736453

jdmsdhfyt_548745

What I want to do is implement this formula : "LEFT(A1,FIND("_",A1) - 1)" in column B. 我要做的是在B列中实现以下公式: "LEFT(A1,FIND("_",A1) - 1)"

The purpose of this is to eliminate the underscores and any characters that come after it. 这样做的目的是消除下划线和其后的所有字符。 I want to be able to apply this formula to the cells in column A and output the new values to column B. I am unable to figure out the proper syntax to approach this problem. 我希望能够将此公式应用于A列中的单元格,并将新值输出到B列。我无法找出解决此问题的正确语法。

First define a function for a single string: 首先为单个字符串定义一个函数:

def splitter(x):
    return x.split('_')[0]

print(splitter('ndjsmdhfr_145874'))
# ndjsmdhfr

Then write to your CSV file. 然后写入您的CSV文件。 Remember to use newline='' as an argument to open . 请记住使用newline=''作为open的参数。 In addition, it's recommended you use the with open(...) construct for reading / writing files. 另外,建议您使用with open(...)构造读取/写入文件。 Here's some pseudo-code: 这是一些伪代码:

with open('out_file.csv', 'w', newline='') as fout:
    w = csv.writer(fout)
    for path, dirs, files in os.walk("/home/niteninja/testfolder"):
        for filename in files:
            w.writerow([filename, splitter(filename)])

These three lines of code will select the first cell in column B and apply the formula, then copy it down the column 这三行代码将选择B列中的第一个单元格并应用公式,然后将其复制到该列中

Range("B1").Select
ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],FIND(""_"",RC[-1]) - 1)"
Selection.AutoFill Range(ActiveCell, ActiveCell.Offset(0, -1).End(xlDown).Offset(0, 1))

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

相关问题 如何将 if...else 子句应用于 Python3 中的整个列? - How do I apply an if...else clause to an entire column in Python3? 如何使用 python 和 openpyxl 将整个 excel 列移动到其当前 position 的左侧或右侧 - How do I move an entire excel column to the left or right of its current position using python and openpyxl 如何对 numpy 中的列执行公式 - How do I perform a formula on a column in numpy 如何从python中的excel列打印字符串值? - How do I print string values from excel column in python? 如果我想删除“,”之前的所有值,如何从整个列中删除字符串的一部分? - How do I remove a part of a string from an entire column, if I want to remove all the values before a “,”? Pandas .apply():如何在 apply() 中使用涉及同一列中前面单元格值的公式? - Pandas .apply(): How to use a formula in apply() that involves values from preceding cells in the same column? 如何替换整个数据集中的值? - How do I replace values in an entire dataset? 如何使用 csv 数据作为变量将其应用于公式? - How do I use a csv data as variables to apply it for a formula? 我如何在整个数据集上应用数据增强 - How do i apply Data Augmentation on entire data-set 是否可以像 GUI 中那样通过 Excel 公式填充整个列? #gspread - Is it possible to fill down the entire column by Excel formula as in GUI? #gspread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM