数据帧中的字符串替换生成“sre_常量。错误:未终止字符集”错误

2024-09-30 12:13:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个csv文件,我正在打开熊猫。 其中一列名为“standardUpc”,包含以下格式的数据:

['45783425568']

我想删除两个字符串:"['""']",以便standardUpc列只有以下值:

45783425568

这是我的密码:

fileloc = "C:/Users/products.csv"
products = pd.read_csv(fileloc)

# COLUMN NAMES ARE: ['productId','title','standardUpc','sellerName','canAddToCart']

# This line only selects conditions where canAddToCart evaluates to True
filtered = (products[(products['canAddToCart'] == True) ])

# Replace the [' and the '] characters in the standardUpc column
filtered["standardUpc"] = filtered["standardUpc"].str.replace("['","")
filtered["standardUpc"] = filtered["standardUpc"].str.replace("']","")

#Now filter further on seller name
filtered_2 =(filtered[(filtered['sellerName'] == 'Robert')])

#print the new dataframe 
for index, row in filtered_2.iterrows(): 
    print (row["productId"], row["sellerName"],row["standardUpc"]) 
#export the filtered dataframe containing the modified UPC string to a new csv file          
filtered_2.to_csv('instock.csv')  

这将在此行生成错误:sre_constants.error: unterminated character set at position 0

filtered["standardUpc"] = filtered["standardUpc"].str.replace("['","")

总结:

我想将CSV文件读入数据帧结构,修改该结构中特定列中的值 dataframe,过滤dataframe中的特定条件,然后将所有这些内容写入新的csv文件

我不想处理数据帧的“视图”。实际上,我正在更改值并将其写入新的CSV文件


Tags: 文件csvtheto数据dataframereplacefiltered
1条回答
网友
1楼 · 发布于 2024-09-30 12:13:32

Pandas对str.replace方法使用正则表达式

这就是您在此行中得到错误sre_constants.error: unterminated character set at position 0的原因

filtered["standardUpc"] = filtered["standardUpc"].str.replace("['","")

对包含正则表达式中具有特殊含义的字符的字符串使用re.escape

import re

import pandas as pd

if __name__ == "__main__":
    L = ['1234', "['456']", '678']

    filtered = pd.DataFrame({'standardUpc': L})
    print(filtered)

    filtered["standardUpc"] = filtered["standardUpc"].str.replace(re.escape("['"), "")
    filtered["standardUpc"] = filtered["standardUpc"].str.replace(re.escape("']"), "")

    print(filtered)

相关问题 更多 >

    热门问题