2017-07-08 1 views
1

Pandas laden Textdateifehler: CParserError: Fehler beim Token von Daten.Pandas laden Textdateifehler: CParserError: Fehler beim Tokenisieren von Daten

Ich bin ein neuer Pandas Lerner. Ich versuche mit Pandas eine Textdatei zu öffnen. Ich schreibe Code in Python, dann auf den richtigen Pfad zugreifen und die Python-Datei ausführen, aber fehlgeschlagen.

Hier sind die Originaldaten. Es gibt keinen Feldnamen, werden alle Zeilen von Daten mit Leerzeichen getrennt sind:

2017-07-02 23:59:51127.0.0.1 GET /ecvv_product/EcvvSearchProduct.aspx cid=202104&p=&pageindex=&kw=electric-skateboard 8082 - 127.0.0.1 Mozilla/4.0+(compatible;+MSIE+5.0;+Windows+NT;+DigExt;+DTS+Agent - 200 0 0 986 31.7.188.55 
2017-07-02 23:59:51 127.0.0.1 GET /ecvv_product/EcvvHotSearchProduct.aspx kw=hydrogen-motor 8082 - 127.0.0.1 Mozilla/4.0+(compatible;+MSIE+5.0;+Windows+NT;+DigExt;+DTS+Agent - 200 0 0 2539 31.7.188.55 
2017-07-02 23:59:51 127.0.0.1 GET /ecvv_product/EcvvSearchProduct.aspx cid=100005713&p=&pageindex=&kw=electric-skateboard 8082 - 127.0.0.1 Mozilla/4.0+(compatible;+MSIE+5.0;+Windows+NT;+DigExt;+DTS+Agent - 200 0 0 1172 31.7.188.55 
2017-07-02 23:59:51 127.0.0.1 GET /ecvv_product/EcvvHotSearchProduct.aspx kw=stainless-stand 8082 - 127.0.0.1 Mozilla/4.0+(compatible;+MSIE+5.0;+Windows+NT;+DigExt;+DTS+Agent - 200 0 0 3152 31.7.188.55 

Hier ist meine einfache Python-Code:

import pandas as pd 

DATA_FILE='data.log' 
df = pd.read_table(DATA_FILE, sep=" ") 

print(df) 

Aber ich habe Fehler wie folgt:

Traceback (most recent call last): 
    File "open.py", line 7, in <module> 
    df = pd.read_table(DATA_FILE, sep=" ") 
    File "C:\Users\hh\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 646, in parser_f 
    return _read(filepath_or_buffer, kwds) 
    File "C:\Users\hh\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 401, in _read 
    data = parser.read() 
    File "C:\Users\hh\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 939, in read 
    ret = self._engine.read(nrows) 
    File "C:\Users\hh\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1508, in read 
    data = self._reader.read(nrows) 
    File "pandas\parser.pyx", line 848, in pandas.parser.TextReader.read (pandas\parser.c:10415) 
    File "pandas\parser.pyx", line 870, in pandas.parser.TextReader._read_low_memory (pandas\parser.c:10691) 
    File "pandas\parser.pyx", line 924, in pandas.parser.TextReader._read_rows (pandas\parser.c:11437) 
    File "pandas\parser.pyx", line 911, in pandas.parser.TextReader._tokenize_rows (pandas\parser.c:11308) 
    File "pandas\parser.pyx", line 2024, in pandas.parser.raise_parser_error (pandas\parser.c:27037) 
pandas.io.common.CParserError: Error tokenizing data. C error: Expected 6 fields in line 4, saw 17 

Es muss etwas mit meinem Python-Code laufen. Wie bekomme ich den richtigen Syntaxcode?

+0

Es ist wie einige der Zeilen in Ihrem Log scheint Datei (nicht diejenigen, die Sie gestellt haben) sind schlecht strukturiert. –

Antwort

0

Sie verfehlten einen Platz in der ersten Zeile:

2017-07-02 23:59:51127.0.0.1 

ersetzen zu:

2017-07-02 23:59:51 127.0.0.1 

gerade getestet:

In [12]: cat data.log 
2017-07-02 23:59:51 127.0.0.1 GET /ecvv_product/EcvvSearchProduct.aspx cid=202104&p=&pageindex=&kw=electric-skateboard 8082 - 127.0.0.1 Mozilla/4.0+(compatible;+MSIE+5.0;+Windows+NT;+DigExt;+DTS+Agent - 200 0 0 986 31.7.188.55 
2017-07-02 23:59:51 127.0.0.1 GET /ecvv_product/EcvvHotSearchProduct.aspx kw=hydrogen-motor 8082 - 127.0.0.1 Mozilla/4.0+(compatible;+MSIE+5.0;+Windows+NT;+DigExt;+DTS+Agent - 200 0 0 2539 31.7.188.55 
2017-07-02 23:59:51 127.0.0.1 GET /ecvv_product/EcvvSearchProduct.aspx cid=100005713&p=&pageindex=&kw=electric-skateboard 8082 - 127.0.0.1 Mozilla/4.0+(compatible;+MSIE+5.0;+Windows+NT;+DigExt;+DTS+Agent - 200 0 0 1172 31.7.188.55 
2017-07-02 23:59:51 127.0.0.1 GET /ecvv_product/EcvvHotSearchProduct.aspx kw=stainless-stand 8082 - 127.0.0.1 Mozilla/4.0+(compatible;+MSIE+5.0;+Windows+NT;+DigExt;+DTS+Agent - 200 0 0 3152 31.7.188.55 

In [13]: dx = pd.read_table('data.log', sep=" ", header=None) 

In [14]: dx 
Out[14]: 
      0   1   2 3 \ 
0 2017-07-02 23:59:51 127.0.0.1 GET 
1 2017-07-02 23:59:51 127.0.0.1 GET 
2 2017-07-02 23:59:51 127.0.0.1 GET 
3 2017-07-02 23:59:51 127.0.0.1 GET 

             4 \ 
0  /ecvv_product/EcvvSearchProduct.aspx 
1 /ecvv_product/EcvvHotSearchProduct.aspx 
2  /ecvv_product/EcvvSearchProduct.aspx 
3 /ecvv_product/EcvvHotSearchProduct.aspx 

                5  6 7   8 \ 
0 cid=202104&p=&pageindex=&kw=electric-skateboard 8082 - 127.0.0.1 
1         kw=hydrogen-motor 8082 - 127.0.0.1 
2 cid=100005713&p=&pageindex=&kw=electric-skateb... 8082 - 127.0.0.1 
3         kw=stainless-stand 8082 - 127.0.0.1 

                9 10 11 12 13 14 \ 
0 Mozilla/4.0+(compatible;+MSIE+5.0;+Windows+NT;... - 200 0 0 986 
1 Mozilla/4.0+(compatible;+MSIE+5.0;+Windows+NT;... - 200 0 0 2539 
2 Mozilla/4.0+(compatible;+MSIE+5.0;+Windows+NT;... - 200 0 0 1172 
3 Mozilla/4.0+(compatible;+MSIE+5.0;+Windows+NT;... - 200 0 0 3152 

      15 
0 31.7.188.55 
1 31.7.188.55 
2 31.7.188.55 
3 31.7.188.55 
+0

oh danke! Ich habe es geändert, aber es hat immer noch nicht funktioniert ... –

+0

Bist du sicher? Ich habe es gerade getestet und es funktioniert. – SayPy

+0

Es funktioniert! Aber für Originaldaten, die fast 193 MB groß sind, tritt derselbe Fehler erneut auf. Bedeutet das, dass die Originaldatei etwas ungültiges hat? –

Verwandte Themen