Home > front end >  Need to ignore lines and replace words in a csv python
Need to ignore lines and replace words in a csv python

Time:02-17

i have to ignore some lines and replace some words in a csv, i tried using the following to replace but it looks like it gets ignored

if "myword" not in line:

to replace text i used

csv_writer.writerow(line.replace("oldword", "newword")) 

but this gets an error does someone knows why? EDIT WITH CODE

import csv

with open(r'excelfile.csv') as csv_file:
    csv_reader = csv.DictReader(csv_file)

    with open('new_names.csv', 'w', newline='') as new_file:
        fieldnames = ['value', 'type', 'description']
        writer=csv.writer(new_file)

        csv_writer =csv.DictWriter(new_file, fieldnames=fieldnames, delimiter= ',', extrasaction='ignore')

        csv_writer.writeheader()

        for line in csv_reader:
                csv_writer.writerow(line)

CodePudding user response:

Because you're using a csv.DictReader, the values are nested in a dictionary structure. For example, if the file has a line that reads:

has myword,and oldword,foo

the line variable will contain:

OrderedDict([('value', 'has myword'), ('type', 'and oldword'), ('description', 'foo')])

Just like @furas suggested in the comments, I determined this by adding print(line) in the for line... loop.

So your logical tests have to be taking that into account.

Here are a few examples that you can adapt to fit your exact purpose:

import csv

with open(r'excelfile.csv') as csv_file:
    csv_reader = csv.DictReader(csv_file)

    with open('new_names.csv', 'w', newline='') as new_file:
        fieldnames = ['value', 'type', 'description']
        writer=csv.writer(new_file)

        csv_writer =csv.DictWriter(new_file, fieldnames=fieldnames, delimiter= ',', extrasaction='ignore')

        csv_writer.writeheader()

        for line in csv_reader:
            # Substitute oldword by newword in all values in line
            line = {k: v.replace("oldword", "newword") for k, v in line.items()}

            # Reject line if any value in line is exactly "myword":
            if "myword" not in line.values():
                csv_writer.writerow(line)

            # Reject line if any value in line contains "myword" as a substring:
            if not any("myword" in value for value in line.values()):
                csv_writer.writerow(line)
  • Related