mercredi 19 février 2020

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. 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. 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?

Thanks so much in advance!

EDIT Sample input:

Sample output

Aucun commentaire:

Enregistrer un commentaire