Pandas Python熊猫:关于文本提取有什么想法吗?

Pandas Python熊猫:关于文本提取有什么想法吗?,pandas,text,extract,separator,Pandas,Text,Extract,Separator,我有数千个txt文件,如下所示(值由以下内容组成): 我想以csv格式提取变量名及其对应的值。幸运的是,正如您所注意到的,所有txt文件都具有类似的格式: variable : [ value ] 我的第一个问题是如何编写一个代码,用上面的结构提取数据 我的第二个问题是,当一行中有多组“variable:[value]”时,我现在知道如何分隔(它们不是逗号分隔的!) 我只想出了下面的代码……但我现在正在兜圈子。有什么想法吗 df = pd.read_csv(filename, sep

我有数千个txt文件,如下所示(值由以下内容组成):

我想以csv格式提取变量名及其对应的值。幸运的是,正如您所注意到的,所有txt文件都具有类似的格式:

variable : [ value ]   
  • 我的第一个问题是如何编写一个代码,用上面的结构提取数据

  • 我的第二个问题是,当一行中有多组“variable:[value]”时,我现在知道如何分隔(它们不是逗号分隔的!)

  • 我只想出了下面的代码……但我现在正在兜圈子。有什么想法吗

    df = pd.read_csv(filename, sep='\n')
    df = df[0].str.split(':', expand=True)
    

    提前感谢

    看起来您需要正则表达式。让我们试试这个

    首先,加载样本数据

    text = \
    """Date :  [ 2010-01-01 XX:XX:XX ]  Age :  [ 22 ]  Sex :  [ M ]   :  [ XXX ]
    Height(cm) :  [ 145 ]  Weight(kg) :  [ 56.4 ]  Race :  [ Hispanic ]
    Spirometry :  [ restrictive pattern ]
    Treatment response :  [ Negative ]
    Tissue volume :  [ Normal ]
    Tissue volume
    [ Normal RV ] 
    Diffusing capacity :  [ Normal capacity ]
    FVC Liters : [ 2.22 ] FVC Liters :  [ 67 ] FVC Liters :  [ 3.35 ] 
    FEV1 Liters :  [ 1.96 ] FEV1 Liters :  [ 66 ] FEV1 Liters :  [ 2.06 ] 
    FEV1 / FVC % :  [ 58 ] FEV1 / FVC % :  [ 62 ]
    DLCO mL/mmHg/min :  [ 21.5 ] DLCO mL/mmHg/min :  [ 102 ]
    DLCO Adj mL/mmHg/min :  [ 21.5 ] DLCO Adj mL/mmHg/min :  [ 102 ]
    RV/TLC % :  [ 22 ]
    """
    
    接下来,使用正则表达式查找所有匹配的“blah:[blahblah]”对,然后插入字典(
    strip
    ed,从空白处删除——本可以放入正则表达式,但要避免过于复杂)

    结果是:

    {'Date': '2010-01-01 XX:XX:XX',
     'Age': '22',
     'Sex': 'M',
     '': 'XXX',
     'Height(cm)': '145',
     'Weight(kg)': '56.4',
     'Race': 'Hispanic',
     'Spirometry': 'restrictive pattern',
     'Treatment response': 'Negative',
     'Tissue volume': 'Normal',
     'Diffusing capacity': 'Normal capacity',
     'FVC Liters': '3.35',
     'FEV1 Liters': '2.06',
     'FEV1 / FVC %': '62',
     'DLCO mL/mmHg/min': '102',
     'DLCO Adj mL/mmHg/min': '102',
     'RV/TLC %': '22'}
    
    如果需要,可以将其粘贴到数据帧中:

    df = pd.DataFrame.from_records([res])
    df
    
    得到

        Date                   Age  Sex           Height(cm)    Weight(kg)  Race      Spirometry           Treatment response    Tissue volume    Diffusing capacity      FVC Liters    FEV1 Liters    FEV1 / FVC %    DLCO mL/mmHg/min    DLCO Adj mL/mmHg/min    RV/TLC %
    --  -------------------  -----  -----  ---  ------------  ------------  --------  -------------------  --------------------  ---------------  --------------------  ------------  -------------  --------------  ------------------  ----------------------  ----------
     0  2010-01-01 XX:XX:XX     22  M      XXX           145          56.4  Hispanic  restrictive pattern  Negative              Normal           Normal capacity               3.35           2.06              62                 102                     102          22
    

    请注意,您提供的示例顶部有一行
    Sex:[M]:[XXX]
    ,这不符合模式,但代码使用空字符串“”作为键来处理它。我假设这是复制粘贴的问题,而不是原始数据中的问题,但是如果您有许多问题,您可能必须更仔细地处理它们

    对于示例数据,要获得键和值,而不使用前导和尾随空格,您可以使用两个捕获组

    ([^\s:][^:]*)\s+\:\s+\[\s*([^][]*)\s+]
    
    • Capturegroup 1
      • [^\s:][^::][^::][*
        匹配除空格字符以外的任何字符或
        后跟除
        以外的可选字符
    • 关闭第1组
    • \s+\:\s+
      在左右两侧的一个或多个空白字符之间匹配
    • \[\s*
      匹配
      [
      和可选空白字符
    • 捕获第2组
      • [^][]*
        匹配除
        [
        ]
    • 关闭第2组
    • \s+]匹配1+空格字符和
      ]`
    |

    输出

    [('Date', '2010-01-01 XX:XX:XX'), ('Age', '22'), ('Sex', 'M'), ('Height(cm)', '145'), ('Weight(kg)', '56.4'), ('Race', 'Hispanic'), ('Spirometry', 'restrictive pattern'), ('Treatment response', 'Negative'), ('Tissue volume', 'Normal'), ('Diffusing capacity', 'Normal capacity'), ('FVC Liters', '2.22'), ('FVC Liters', '67'), ('FVC Liters', '3.35'), ('FEV1 Liters', '1.96'), ('FEV1 Liters', '66'), ('FEV1 Liters', '2.06'), ('FEV1 / FVC %', '58'), ('FEV1 / FVC %', '62'), ('DLCO mL/mmHg/min', '21.5'), ('DLCO mL/mmHg/min', '102'), ('DLCO Adj mL/mmHg/min', '21.5'), ('DLCO Adj mL/mmHg/min', '102'), ('RV/TLC %', '22')]
    

    感谢您的详细指导!我以前不知道正则表达式是什么。我来试试你的解决方案谢谢你的主意!我将把北斗七星挖掘到正则表达式中
    ([^\s:][^:]*)\s+\:\s+\[\s*([^][]*)\s+]
    
    [('Date', '2010-01-01 XX:XX:XX'), ('Age', '22'), ('Sex', 'M'), ('Height(cm)', '145'), ('Weight(kg)', '56.4'), ('Race', 'Hispanic'), ('Spirometry', 'restrictive pattern'), ('Treatment response', 'Negative'), ('Tissue volume', 'Normal'), ('Diffusing capacity', 'Normal capacity'), ('FVC Liters', '2.22'), ('FVC Liters', '67'), ('FVC Liters', '3.35'), ('FEV1 Liters', '1.96'), ('FEV1 Liters', '66'), ('FEV1 Liters', '2.06'), ('FEV1 / FVC %', '58'), ('FEV1 / FVC %', '62'), ('DLCO mL/mmHg/min', '21.5'), ('DLCO mL/mmHg/min', '102'), ('DLCO Adj mL/mmHg/min', '21.5'), ('DLCO Adj mL/mmHg/min', '102'), ('RV/TLC %', '22')]