當前位置: 首頁>>代碼示例>>Python>>正文


Python parsers.read_fwf方法代碼示例

本文整理匯總了Python中pandas.io.parsers.read_fwf方法的典型用法代碼示例。如果您正苦於以下問題:Python parsers.read_fwf方法的具體用法?Python parsers.read_fwf怎麽用?Python parsers.read_fwf使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pandas.io.parsers的用法示例。


在下文中一共展示了parsers.read_fwf方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_basic

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_basic():
    data = """\
A         B            C            D
201158    360.242940   149.910199   11950.7
201159    444.953632   166.985655   11788.4
201160    364.136849   183.628767   11806.2
201161    413.836124   184.375703   11916.8
201162    502.953953   173.237159   12468.3
"""
    result = read_fwf(StringIO(data))
    expected = DataFrame([[201158, 360.242940, 149.910199, 11950.7],
                          [201159, 444.953632, 166.985655, 11788.4],
                          [201160, 364.136849, 183.628767, 11806.2],
                          [201161, 413.836124, 184.375703, 11916.8],
                          [201162, 502.953953, 173.237159, 12468.3]],
                         columns=["A", "B", "C", "D"])
    tm.assert_frame_equal(result, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:19,代碼來源:test_read_fwf.py

示例2: test_colspecs

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_colspecs():
    data = """\
A   B     C            D            E
201158    360.242940   149.910199   11950.7
201159    444.953632   166.985655   11788.4
201160    364.136849   183.628767   11806.2
201161    413.836124   184.375703   11916.8
201162    502.953953   173.237159   12468.3
"""
    colspecs = [(0, 4), (4, 8), (8, 20), (21, 33), (34, 43)]
    result = read_fwf(StringIO(data), colspecs=colspecs)

    expected = DataFrame([[2011, 58, 360.242940, 149.910199, 11950.7],
                          [2011, 59, 444.953632, 166.985655, 11788.4],
                          [2011, 60, 364.136849, 183.628767, 11806.2],
                          [2011, 61, 413.836124, 184.375703, 11916.8],
                          [2011, 62, 502.953953, 173.237159, 12468.3]],
                         columns=["A", "B", "C", "D", "E"])
    tm.assert_frame_equal(result, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:21,代碼來源:test_read_fwf.py

示例3: test_widths

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_widths():
    data = """\
A    B    C            D            E
2011 58   360.242940   149.910199   11950.7
2011 59   444.953632   166.985655   11788.4
2011 60   364.136849   183.628767   11806.2
2011 61   413.836124   184.375703   11916.8
2011 62   502.953953   173.237159   12468.3
"""
    result = read_fwf(StringIO(data), widths=[5, 5, 13, 13, 7])

    expected = DataFrame([[2011, 58, 360.242940, 149.910199, 11950.7],
                          [2011, 59, 444.953632, 166.985655, 11788.4],
                          [2011, 60, 364.136849, 183.628767, 11806.2],
                          [2011, 61, 413.836124, 184.375703, 11916.8],
                          [2011, 62, 502.953953, 173.237159, 12468.3]],
                         columns=["A", "B", "C", "D", "E"])
    tm.assert_frame_equal(result, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:20,代碼來源:test_read_fwf.py

示例4: test_non_space_filler

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_non_space_filler():
    # From Thomas Kluyver:
    #
    # Apparently, some non-space filler characters can be seen, this is
    # supported by specifying the 'delimiter' character:
    #
    # http://publib.boulder.ibm.com/infocenter/dmndhelp/v6r1mx/index.jsp?topic=/com.ibm.wbit.612.help.config.doc/topics/rfixwidth.html
    data = """\
A~~~~B~~~~C~~~~~~~~~~~~D~~~~~~~~~~~~E
201158~~~~360.242940~~~149.910199~~~11950.7
201159~~~~444.953632~~~166.985655~~~11788.4
201160~~~~364.136849~~~183.628767~~~11806.2
201161~~~~413.836124~~~184.375703~~~11916.8
201162~~~~502.953953~~~173.237159~~~12468.3
"""
    colspecs = [(0, 4), (4, 8), (8, 20), (21, 33), (34, 43)]
    result = read_fwf(StringIO(data), colspecs=colspecs, delimiter="~")

    expected = DataFrame([[2011, 58, 360.242940, 149.910199, 11950.7],
                          [2011, 59, 444.953632, 166.985655, 11788.4],
                          [2011, 60, 364.136849, 183.628767, 11806.2],
                          [2011, 61, 413.836124, 184.375703, 11916.8],
                          [2011, 62, 502.953953, 173.237159, 12468.3]],
                         columns=["A", "B", "C", "D", "E"])
    tm.assert_frame_equal(result, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:27,代碼來源:test_read_fwf.py

示例5: test_read_csv_compat

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_read_csv_compat():
    csv_data = """\
A,B,C,D,E
2011,58,360.242940,149.910199,11950.7
2011,59,444.953632,166.985655,11788.4
2011,60,364.136849,183.628767,11806.2
2011,61,413.836124,184.375703,11916.8
2011,62,502.953953,173.237159,12468.3
"""
    expected = read_csv(StringIO(csv_data), engine="python")

    fwf_data = """\
A   B     C            D            E
201158    360.242940   149.910199   11950.7
201159    444.953632   166.985655   11788.4
201160    364.136849   183.628767   11806.2
201161    413.836124   184.375703   11916.8
201162    502.953953   173.237159   12468.3
"""
    colspecs = [(0, 4), (4, 8), (8, 20), (21, 33), (34, 43)]
    result = read_fwf(StringIO(fwf_data), colspecs=colspecs)
    tm.assert_frame_equal(result, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:24,代碼來源:test_read_fwf.py

示例6: test_fwf_for_uint8

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_fwf_for_uint8():
    data = """1421302965.213420    PRI=3 PGN=0xef00      DST=0x17 SRC=0x28    04 154 00 00 00 00 00 127
1421302964.226776    PRI=6 PGN=0xf002               SRC=0x47    243 00 00 255 247 00 00 71"""  # noqa
    df = read_fwf(StringIO(data),
                  colspecs=[(0, 17), (25, 26), (33, 37),
                            (49, 51), (58, 62), (63, 1000)],
                  names=["time", "pri", "pgn", "dst", "src", "data"],
                  converters={
                      "pgn": lambda x: int(x, 16),
                      "src": lambda x: int(x, 16),
                      "dst": lambda x: int(x, 16),
                      "data": lambda x: len(x.split(" "))})

    expected = DataFrame([[1421302965.213420, 3, 61184, 23, 40, 8],
                          [1421302964.226776, 6, 61442, None, 71, 8]],
                         columns=["time", "pri", "pgn",
                                  "dst", "src", "data"])
    expected["dst"] = expected["dst"].astype(object)
    tm.assert_frame_equal(df, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:21,代碼來源:test_read_fwf.py

示例7: test_dtype

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_dtype(dtype):
    data = """ a    b    c
1    2    3.2
3    4    5.2
"""
    colspecs = [(0, 5), (5, 10), (10, None)]
    result = read_fwf(StringIO(data), colspecs=colspecs, dtype=dtype)

    expected = pd.DataFrame({
        "a": [1, 3], "b": [2, 4],
        "c": [3.2, 5.2]}, columns=["a", "b", "c"])

    for col, dt in dtype.items():
        expected[col] = expected[col].astype(dt)

    tm.assert_frame_equal(result, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:18,代碼來源:test_read_fwf.py

示例8: test_fwf_compression

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_fwf_compression(compression_only, infer):
    data = """1111111111
    2222222222
    3333333333""".strip()

    compression = compression_only
    extension = "gz" if compression == "gzip" else compression

    kwargs = dict(widths=[5, 5], names=["one", "two"])
    expected = read_fwf(StringIO(data), **kwargs)

    if compat.PY3:
        data = bytes(data, encoding="utf-8")

    with tm.ensure_clean(filename="tmp." + extension) as path:
        tm.write_to_compressed(compression, path, data)

        if infer is not None:
            kwargs["compression"] = "infer" if infer else compression

        result = read_fwf(path, **kwargs)
        tm.assert_frame_equal(result, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:24,代碼來源:test_read_fwf.py

示例9: test_fwf_colspecs_None

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_fwf_colspecs_None(self):
        # GH 7079
        data = """\
123456
456789
"""
        colspecs = [(0, 3), (3, None)]
        result = read_fwf(StringIO(data), colspecs=colspecs, header=None)
        expected = DataFrame([[123, 456], [456, 789]])
        tm.assert_frame_equal(result, expected)

        colspecs = [(None, 3), (3, 6)]
        result = read_fwf(StringIO(data), colspecs=colspecs, header=None)
        expected = DataFrame([[123, 456], [456, 789]])
        tm.assert_frame_equal(result, expected)

        colspecs = [(0, None), (3, None)]
        result = read_fwf(StringIO(data), colspecs=colspecs, header=None)
        expected = DataFrame([[123456, 456], [456789, 789]])
        tm.assert_frame_equal(result, expected)

        colspecs = [(None, None), (3, 6)]
        result = read_fwf(StringIO(data), colspecs=colspecs, header=None)
        expected = DataFrame([[123456, 456], [456789, 789]])
        tm.assert_frame_equal(result, expected) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:27,代碼來源:test_read_fwf.py

示例10: test_fwf_compression

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_fwf_compression(self):
        try:
            import gzip
            import bz2
        except ImportError:
            pytest.skip("Need gzip and bz2 to run this test")

        data = """1111111111
        2222222222
        3333333333""".strip()
        widths = [5, 5]
        names = ['one', 'two']
        expected = read_fwf(StringIO(data), widths=widths, names=names)
        if compat.PY3:
            data = bytes(data, encoding='utf-8')
        comps = [('gzip', gzip.GzipFile), ('bz2', bz2.BZ2File)]
        for comp_name, compresser in comps:
            with tm.ensure_clean() as path:
                tmp = compresser(path, mode='wb')
                tmp.write(data)
                tmp.close()
                result = read_fwf(path, widths=widths, names=names,
                                  compression=comp_name)
                tm.assert_frame_equal(result, expected) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:26,代碼來源:test_read_fwf.py

示例11: test_over_specified

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_over_specified():
    data = """\
A   B     C            D            E
201158    360.242940   149.910199   11950.7
201159    444.953632   166.985655   11788.4
201160    364.136849   183.628767   11806.2
201161    413.836124   184.375703   11916.8
201162    502.953953   173.237159   12468.3
"""
    colspecs = [(0, 4), (4, 8), (8, 20), (21, 33), (34, 43)]

    with pytest.raises(ValueError, match="must specify only one of"):
        read_fwf(StringIO(data), colspecs=colspecs, widths=[6, 10, 10, 7]) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:15,代碼來源:test_read_fwf.py

示例12: test_bytes_io_input

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_bytes_io_input():
    if not compat.PY3:
        pytest.skip("Bytes-related test - only needs to work on Python 3")

    result = read_fwf(BytesIO("שלום\nשלום".encode('utf8')),
                      widths=[2, 2], encoding="utf8")
    expected = DataFrame([["של", "ום"]], columns=["של", "ום"])
    tm.assert_frame_equal(result, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:10,代碼來源:test_read_fwf.py

示例13: test_fwf_colspecs_is_list_or_tuple

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_fwf_colspecs_is_list_or_tuple():
    data = """index,A,B,C,D
foo,2,3,4,5
bar,7,8,9,10
baz,12,13,14,15
qux,12,13,14,15
foo2,12,13,14,15
bar2,12,13,14,15
"""

    msg = "column specifications must be a list or tuple.+"

    with pytest.raises(TypeError, match=msg):
        read_fwf(StringIO(data), colspecs={"a": 1}, delimiter=",") 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:16,代碼來源:test_read_fwf.py

示例14: test_fwf_colspecs_is_list_or_tuple_of_two_element_tuples

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_fwf_colspecs_is_list_or_tuple_of_two_element_tuples():
    data = """index,A,B,C,D
foo,2,3,4,5
bar,7,8,9,10
baz,12,13,14,15
qux,12,13,14,15
foo2,12,13,14,15
bar2,12,13,14,15
"""

    msg = "Each column specification must be.+"

    with pytest.raises(TypeError, match=msg):
        read_fwf(StringIO(data), [("a", 1)]) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:16,代碼來源:test_read_fwf.py

示例15: test_fwf_colspecs_none

# 需要導入模塊: from pandas.io import parsers [as 別名]
# 或者: from pandas.io.parsers import read_fwf [as 別名]
def test_fwf_colspecs_none(colspecs, exp_data):
    # see gh-7079
    data = """\
123456
456789
"""
    expected = DataFrame(exp_data)

    result = read_fwf(StringIO(data), colspecs=colspecs, header=None)
    tm.assert_frame_equal(result, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:12,代碼來源:test_read_fwf.py


注:本文中的pandas.io.parsers.read_fwf方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。