-2

I have a CSV file containing emails in which i need to validate if there are more than one columns, if there are then i need to reject the file, I was thinking of reading each line and using "in" operation to check if first row contains comma , although in one of the yahoo link https://in.answers.yahoo.com/question/index?qid=20101211183849AAy3gVh i found that we can have commas in email address if in double quotes.

What should be the logic here?

can't use csv or pandas module

first 3 rows of file:

dilkash.manghani@gmail.com, dilkash.manghani@gmail.com
dilkash123@gmail.com, dilkash123@gmail.com
dilkash423@gmail.com, dilkash523@gmail.com

code:

with open("file.csv", "r") as f:
    data = f.read().split('\n')
    for email in data:
        if "," in email:
            raise Exception("invalid")
dilkash
  • 562
  • 3
  • 15
  • 1
    What have you tried so far? Please post your code – James Jul 25 '18 at 17:08
  • i read the file, parsed each row and verified if there is comma in any row, not sure why it is down voted though, does somebody know the answer – dilkash Jul 25 '18 at 17:15
  • show us how your input file looks like, and show what you have tried till now – rawwar Jul 25 '18 at 17:17
  • added first few lines of file – dilkash Jul 25 '18 at 17:19
  • @dilkash, it can't be a valid csv, if it includes commas in a column then,it should be surrounded by a double quote – rawwar Jul 25 '18 at 17:22
  • Why don't you simply discard any quoted strings, then look for any remaining commas? `sed -n 's/"[^"]*"//g;/,/p' filename` (This is arguably still too crude for all the weird wonderful corner cases of RFC 5322, but might still be good enough in practice.) – tripleee Jul 25 '18 at 17:30

2 Answers2

0

use pandas, load into a data frame as below

import pandas 

df = pandas.read_csv(file_path)
if (len(df.columns) >2):
    raise Exception
else:
    print("Valid")

EDIT: Since, you can't use pandas or csv, you can do the following

with open(file) as f:
    for each in f:
        if len(f.split(',"')) > 2:
            raise Exception
    print("Valid CSV")

Note: i used ," as the separator. why?, because, since if a comma can be included in a column, it is supposed to be surrounded by double quotes. And since its a csv, each column has to be separated by a comma. hence, it would atleast have one ,"

rawwar
  • 4,834
  • 9
  • 32
  • 57
-1
>>> import csv
>>> with open('eggs.csv', 'rb') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
...     for row in spamreader:
...         assert(len(row) == 1)
...         print ', '.join(row)

will do the job.

Jonathan R
  • 3,652
  • 3
  • 22
  • 40