Home > OS >  Check string formating
Check string formating

Time:10-12

I need to check the first value on a csv file. It need to match an specific string. "ddd.ddddd" all numbers. All CSV values start and end with ""

Sample string from CSV

"123.12345"

The code so far is

import re
import csv

strformat = re.compile('\"\d{3}\.\d{5}\"')

with open(final_file, newline='') as fin:
    for f in csv.reader(fin[0]):
        if strformat is not None:
            print ("Match")
        else:
            print ("No Match")

CodePudding user response:

You need to reverse your code like this,

# The expression is changed
strformat = re.compile(r'\d{3}\.\d{5}') 

with open(final_file, newline='') as fin:
    for row in csv.reader(fin):
        # You have to check match like this
        if strformat.match(row[0]):
            print ("Match")
        else:
            print ("No Match")
  • Related