首页 > 解决方案 > 读取 row_python 中的每个字符串

问题描述

我有一个 df,其中列“元素”如下所示: 在此处输入图像描述

如何使用它作为分隔符“|”分割每个单词 然后将每个单词作为避免重复的键发送?我使用了这个,但它不起作用:

    for row in df.elements.itertuples():
       for l in row.elements.split("|"):
           list_elem.send_keys(l)

标签: pythonpandassplit

解决方案


根据需要修改:

#for each row in the frame
for index, row in df.iterrows():
    #if blank skip it
    if row['elements'] == '':
        continue
    #if '|' isn't in the string skip it too
    elif '|' not in row['elements']:
        continue
    else:
        #split the string into a list by the the '|' char
        l = row['elements'].split('|')
        #remove whitespace from the beginning and end of each item
        l = [x.strip() for x in l]
        #for each item in the list
        for i, item in enumerate(l):
            #if the first item
            if i == 0:
                #send; print for testing
                print(item)
            #if not the first item
            else:
                #send; print (for testing) first and current item; 
                #break this into two lines if required
                print(l[0], item)

输出:

Plastica
Plastica plastica
Plastica plastic
Metallo
Metallo metallo
Acciaio inossidabile
Acciaio inossidabile acciaio inossidabile
Acciaio inossidabile acciaio
Acciaio inossidabile albero della gomma

推荐阅读