【问题标题】:Converting List string or string to dataframe python将列表字符串或字符串转换为数据框python
【发布时间】:2022-08-17 20:48:52
【问题描述】:

我有一个捕获信息的代码,我需要使用 pytesseract 和 cv2 将此信息的输出转换为数据帧

输出格式如下:

    print(data)
    print(type(data))
    chats.append(data)
    print(chats)
    print(type(chats))

导致

Preco(USDT) Quantia(BTC) Total
23624.21 0.00617 145.76138
23624.04 0.02000 472.48080
23624.00 0.00100 YRS tt)
23623.60 0.00650 153.55340
23623.37 0.00842 198.90878
23623.36 0.00846 199.85363
23623.28 0.00636 150.24406
23623.27 0.01913 451.91316
23623.01 0.00640 151.18726
23622.98 0.00675 159.45512
23622.85 0.00052 12.28388
23622.84 0.00210 49.60796

<class \'str\'>
[\'Preco(USDT) Quantia(BTC) Total\\n23624.21 0.00617 145.76138\\n23624.04 0.02000 472.48080\\n23624.00 0.00100 YRS tt)\\n23623.60 0.00650 153.55340\\n23623.37 0.00842 198.90878\\n23623.36 0.00846 199.85363\\n23623.28 0.00636 150.24406\\n23623.27 0.01913 451.91316\\n23623.01 0.00640 151.18726\\n23622.98 0.00675 159.45512\\n23622.85 0.00052 12.28388\\n23622.84 0.00210 49.60796\\n\']
<class \'list\'>

如何将一个或两个输出转换为数据框?

    标签: python pandas dataframe


    【解决方案1】:

    使用string.IO

    from io import StringIO
    
    TESTDATA = StringIO("""Preco(USDT) Quantia(BTC) Total
    23624.21 0.00617 145.76138
    23624.04 0.02000 472.48080
    23624.00 0.00100 YRS
    23623.60 0.00650 153.55340
    23623.37 0.00842 198.90878
    23623.36 0.00846 199.85363
    23623.28 0.00636 150.24406
    23623.27 0.01913 451.91316
    23623.01 0.00640 151.18726
    23622.98 0.00675 159.45512
    23622.85 0.00052 12.28388
    23622.84 0.00210 49.60796""".replace(' ', ','))
    
    df = pd.read_csv(TESTDATA,)
    df
    

    【讨论】:

      猜你喜欢
      • 2019-04-28
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 2021-12-04
      • 2014-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多