0

I am writing python script in which i am generating two different csv files and then reading these file by using pandas. I am able to read file1 with pandas but getting error while reading file2 which in same format(same column name) as file1 but different/same values. Please find the below error that i am getting and sample code that i am using.

Error:

Traceback (most recent call last):
  File "MSReport.py", line 168, in <module>
    fail = pd.read_csv('/home/cisapp/msLogFailure.csv', sep=',')
  File "/home/cisapp/.local/lib/python3.6/site-packages/pandas/io/parsers.py", line 676, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/home/cisapp/.local/lib/python3.6/site-packages/pandas/io/parsers.py", line 448, in _read
    parser = TextFileReader(fp_or_buf, **kwds)
  File "/home/cisapp/.local/lib/python3.6/site-packages/pandas/io/parsers.py", line 880, in __init__
    self._make_engine(self.engine)
  File "/home/cisapp/.local/lib/python3.6/site-packages/pandas/io/parsers.py", line 1114, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "/home/cisapp/.local/lib/python3.6/site-packages/pandas/io/parsers.py", line 1891, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas/_libs/parsers.pyx", line 532, in pandas._libs.parsers.TextReader.__cinit__
pandas.errors.EmptyDataError: No columns to parse from file

Code:

df = pd.read_csv(BASE_LOCATION+'/msLog_Success.csv', engine='python')
f_output = df.groupby('MSISDN').last()
#print(df)
print(f_output)
fail = pd.read_csv(BASE_LOCATION+'/msLogFailure.csv', engine='python')
fail = fail['MSISDN']
fail = fail.tolist()
for i in fail:
    succ = f_output[f_output.MSISDN != i]

In above sample code there is no error while reading file df = pd.read_csv(BASE_LOCATION+'/msLog_Success.csv', engine='python') but while reading file fail = pd.read_csv(BASE_LOCATION+'/msLogFailure.csv', engine='python') i am facing the error as mentioned above. Please help to resolve.

Note: I am running code by using python3.

Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55
Himanshu
  • 21
  • 1
  • 5
  • https://stackoverflow.com/questions/40193452/importing-text-file-no-columns-to-parse-from-file - This might help – Mahendra Singh May 06 '20 at 06:03
  • Check if file exists `msLogFailure` might be a typo. Maybe `msLog_Failure`? – Vishnudev Krishnadas May 06 '20 at 06:06
  • Please include at least a few initial lines form *msLogFailure.csv* file. Take also a look at what separators are used between fields. As you didn't pass *sep* parameter, they should be just commas. – Valdi_Bo May 06 '20 at 07:41
  • @Valdi_Bo Please find below file output with headers of msLogFailure.csv. Both success and failure file have same format. MSISDN,REQUEST_ID,STATE1,STATE2,STAETE3,NOTIFICATION 22969000034,OFFLINE_Notification_10.10.46.95_61755e28-f6a1-43de-ae2c-bad308c23e68,ENTERED,IN_PROGRESS,FAILURE,Successfully send SMS to the subscriber – Himanshu May 06 '20 at 08:42

2 Answers2

0

I faced the same problem and resolved. So you can check using below idea.

Check the delimitator and mention like below examples

  1. pd.read_csv(BASE_LOCATION+'/msLog_Success.csv', encoding='utf-16', sep='\t')
  2. pd.read_csv(BASE_LOCATION+'/msLog_Success.csv', delim_whitespace=True)

You can also add 'r' before file path.

Otherwise share the file image

0

Your sample of msLogFailure file looks OK - 6 column names and 6 data fields.

I looked for posts concerning just this error message and I found an advice to:

  • read the input file into a string variable,
  • read_csv from this string, e.g. pd.read_csv(io.StringIO(txt),...).

Maybe this will help.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41