简体   繁体   English

查找第一次出现并在没有出现时返回 0 然后将新列附加到 csv (python) 的代码

[英]Code to find first occurence and return 0 if no occurence and then append new column onto csv (python)

I am trying to extract values inside a column in a csv.我正在尝试从 csv 中的列中提取值。 Here is my code so far:到目前为止,这是我的代码:

```code```
import csv
with open('Cash_Statement_Pier21 accounts 2019c.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
    for line in row:
        index = line.find('@')
        if index != -1:
            print(line[index+2:index+7])
        else:
            index = line.find('RATE')
            if index != -1:
                print(line[index +2])

However, this code fails for lines with multiple "@"s and it only prints out the list in python.但是,对于具有多个“@”的行,此代码失败,它仅在 python 中打印出列表。 How would I append this code such that it a) only takes into account the first "@" on every line and b) creates a new column in my csv?我将如何附加此代码,以便 a) 只考虑每一行的第一个“@”,并且 b) 在我的 csv 中创建一个新列?

Thanks so much in advance!非常感谢!

EDIT SAMPLE INPUT ( seperated lines for readability) NOVO NORDISK A/S 216757.000 SHS @ 5.15000000 EXDTE-22MAR19 PAYDTE-26MAR19 TAXABLE RECLAIMABLE TAXED AT .270000%编辑样本输入(为了可读性而用分隔线)NOVO NORDISK A/S 216757.000 SHS @ 5.15000000 EXDTE-22MAR19 PAYDTE-26MAR19 TAXABLE RECLAIMABLE TAXED at .270000%

NOVO NORDISK A/S 205395.000 SHS @ 3.00000000 EXDTE-16AUG19 PAYDTE-20AUG19 TAXABLE RECLAIMABLE TAXED AT .270000% NOVO NORDISK A/S 205395.000 SHS @ 3.00000000 EXDTE-16AUG19 PAYDTE-20AUG19 TAXABLE RECLAIMABLE TAXABLE 0.270000%

NOVO NORDISK A/S TAX RECLAIM PAID 79000.000 EX-DT:20MAR15 PY-DT:24MAR15 CURRENCY TIP # 1150790014348 NOVO NORDISK A/S TAX RECLAIM PAID 79000.000 EX-DT:20MAR15 PY-DT:24MAR15 CURRENCY TIP # 1150790014348

PING AN INSURANCE GROUP CO OF HK/02318 1184000.000 SHS @ 1.28414399EXDTE-23MAY19 PAYDTE-28JUN19 TAXABLE AT ISSUE RATE .100000% PING AN INSURANCE GROUP CO OF HK/02318 1184000.000 SHS @ 1.28414399EXDTE-23MAY19 PAYDTE-28JUN19 按发行利率计税 0.100000%

BANK CENTRAL ASIA TBK PT    3314200.000 SHS @ 100.00000000 EXDTE-06DEC19 PAYDTE-20DEC19 TAXABLE AT ISSUE RATE .200000%

样品输出

There are 2 different problems here.这里有两个不同的问题。

  1. How to only process first @ or each line?如何只处理第一个@或每一行?

    I will assume that despite what you have shown, you are actually processing a true csv file with more than one field per row and only want to process the first field containing a @ caracter.我会假设,不管你已经展示了什么,你实际上是在处理一个真正的 csv 文件,每行有一个以上的字段,并且只想处理包含@字符的第一个字段 If you do not want to search for RATE if you have found @ , it is trivial: just add a break statement:如果您在找到@不想搜索RATE ,这很简单:只需添加一个break语句:

     for row in csv_reader: for line in row: index = line.find('@') if index != -1: print(line[index+2:index+7]) break // ignore any other field in that row else: ...

    If you want to process the first @ and the first RATE on each row, you will have to use a boolean to ignore any @ but the first, but still continue up to a RATE :如果要处理每一行的第一个@第一个RATE ,则必须使用布尔值来忽略除第一个以外的任何@ ,但仍继续处理RATE

     for row in csv_reader: first = True for line in row: if first: index = line.find('@') if index != -1: print(line[index+2:index+7]) first = False // ignore any other field in that row else: ...
  2. How to add a new field to a CSV?如何向 CSV 添加新字段?

    A CSV is a text file. CSV 是一个文本文件。 The only reliable way to change it is to rewrite everything on a temporary file and when done remove the old file (or rename it as a backup) and rename the temporary to the original name.更改它的唯一可靠方法是重写临时文件上的所有内容,完成后删除旧文件(或将其重命名为备份)并将临时文件重命名为原始名称。 More or less (only showing high level):或多或少(只显示高水平):

     ok = False with open('Cash_Statement_Pier21 accounts 2019c.csv') as csv_file: with open('Cash_Statement_Pier21 accounts 2019c.tmp', 'w',', newline='') as out_file: csv_reader = csv.reader(csv_file, delimiter=',') csv_writer = csv.writer(out_file, delimiter=') for row in csv_reader: ... row.append(new_column_value) csv_writer.writerow(row) ok = True if ok: // do not rename if an error occured... os.remove('Cash_Statement_Pier21 accounts 2019c.csv') os.rename('Cash_Statement_Pier21 accounts 2019c.tmp', 'Cash_Statement_Pier21 accounts 2019c.csv)'

    We have to wait after the end of the with block to execute the rename, because some OS may refuse to remove or rename an open file.我们必须在with块结束后等待执行重命名,因为某些操作系统可能拒绝删除或重命名打开的文件。

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

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