M S
M S

Reputation: 944

Converting only a individual single letter from lowercase to uppercase in python csv reader

I want to convert all the i (in lower case) to its upper case, i.e. (I). I am merging 2 rows of a csv file and printing it. I want to replace all the individual characters (i) to their uppercase form (I). This should not be applied to other strings in the text file, like is, itself, it, in etc. I have tried, but not getting the desired output. Any help is deeply appreciated.

import csv, string, re, nltk
def process_reqs():
    with open('res.csv') as f:
       reader = csv.reader(f)
       next(reader, None)
       global raw_text
       with open('raw_res.txt', 'w', encoding = 'utf-8') as f1:
          rows = ('"{}."'.format(' '.join(row)) for row in reader)
          raw_text = ', '.join(rows)
          for word in raw_text.split():
              if word == 'i':
                 raw_text = raw_text.replace(word, "I")
          f1.write(raw_text)
          print(raw_text)
process_reqs()

Upvotes: 0

Views: 193

Answers (1)

Nicolas Gervais
Nicolas Gervais

Reputation: 36624

You can do a simple string replacement. Just don't forget the spaces, i.e replace (" i ") by (" I ").

Upvotes: 1

Related Questions