在Python中跳过CSV的第一行 - 不起作用?为什么?

时间:2018-03-13 18:35:21

标签: python pandas csv rows readfile

我的csv数据如下所示:

$('table').each(function(index) {
    $(this).find('tr').each(function() {
      $(this).find('td:first-child').each(function(i) { 
        var a;
        var b;
        var c;
        $(this).find('a:first').each(function(i) { 
          a = $(this).text();
        });
        $(this).find('p:first').each(function(i) { 
          b = ($(this).text());
        });
        $(this).find('time:first').each(function(i) { 
          c = $(this).text();
        });
        console.log(a + ";"  + b + ";" + c);
      });
    });
  });
XA452:01 Description in Column 1;ID Column1;13.03.2018
AY102:22 Description in Column 2;ID Column2;13.03.2018
BC001:31 Description in Column 3;ID Column3;13.03.2018
DE223:34 Description in Column 4;ID Column4;13.03.2018
FG315:56 Description in Column 5;ID Column5;13.03.2018
HA212:34 Description in Column 6;ID Column6;13.03.2018
EE111:12 Description in Column 7;ID Column7;13.03.2018

我想开始解析行以XA452:01开头的数据。

我试过了:

import pandas as pd

testimport_data = pd.read_csv("C:/Users/fff/Desktop/test_data.txt", sep=";", skiprows = 19)

print(testimport_data)

应该有用,不应该吗?但是,我收到以下错误消息:

Traceback (most recent call last):
  File "C:/Users/fff/PycharmProjects/Test/Test.py", line 3, in <module>
    testimport_data = pd.read_csv("C:/Users/fff/Desktop/test_data", sep=";", skiprows = 19)
  File "C:\Users\fff\PycharmProjects\Test\venv\lib\site-packages\pandas\io\parsers.py", line 709, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\fff\PycharmProjects\Test\venv\lib\site-packages\pandas\io\parsers.py", line 449, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Users\fff\PycharmProjects\Test\venv\lib\site-packages\pandas\io\parsers.py", line 818, in __init__
    self._make_engine(self.engine)
  File "C:\Users\fff\PycharmProjects\Test\venv\lib\site-packages\pandas\io\parsers.py", line 1049, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "C:\Users\fff\PycharmProjects\Test\venv\lib\site-packages\pandas\io\parsers.py", line 1695, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas\_libs\parsers.pyx", line 565, in pandas._libs.parsers.TextReader.__cinit__
pandas.errors.EmptyDataError: No columns to parse from file

我做错了什么?

2 个答案:

答案 0 :(得分:2)

你可以低头阅读那条线。

import pandas as pd

with open('pd_csv.csv') as f:
    data = [line.split(";") for line in f.readlines()[19:]]

testimport_data = pd.DataFrame(data)
print(testimport_data)

答案 1 :(得分:1)

编辑:拉胡尔指出,我在下面所说的并不完全正确。问题出现,因为跳过的文本包括分号。因此,我将分号切换为昏迷修复了此问题,因为跳过的文本中没有逗号。

旧文: 这是一个问题;作为分隔符。为什么我不确定,但如果你切换文本文件';'到','那么程序运行正常。

过去我通过改变引擎熊猫用来读取文件的方式来完成这项工作,如果你需要它使用半冒号。

我使用的文件名为values.txt

$('table').each(function(index) {
    $(this).find('tr').each(function() {
      $(this).find('td:first-child').each(function(i) { 
        var a;
        var b;
        var c;
        $(this).find('a:first').each(function(i) { 
          a = $(this).text();
        });
        $(this).find('p:first').each(function(i) { 
          b = ($(this).text());
        });
        $(this).find('time:first').each(function(i) { 
          c = $(this).text();
        });
        console.log(a + ";"  + b + ";" + c);
      });
    });
  });
XA452:01 Description in Column 1,ID Column1,13.03.2018
AY102:22 Description in Column 2,ID Column2,13.03.2018
BC001:31 Description in Column 3,ID Column3,13.03.2018
DE223:34 Description in Column 4,ID Column4,13.03.2018
FG315:56 Description in Column 5,ID Column5,13.03.2018
HA212:34 Description in Column 6,ID Column6,13.03.2018
EE111:12 Description in Column 7,ID Column7,13.03.2018

然后用:

运行它
>>> data = pd.read_csv('values.txt', sep=',', skiprows=19, names=['1', '2', '3'])
>>> data
                                  1           2           3
0  XA452:01 Description in Column 1  ID Column1  13.03.2018
1  AY102:22 Description in Column 2  ID Column2  13.03.2018
2  BC001:31 Description in Column 3  ID Column3  13.03.2018
3  DE223:34 Description in Column 4  ID Column4  13.03.2018
4  FG315:56 Description in Column 5  ID Column5  13.03.2018
5  HA212:34 Description in Column 6  ID Column6  13.03.2018
6  EE111:12 Description in Column 7  ID Column7  13.03.2018