繁体   English   中英

从字符串中删除“

Remove " from string

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

with open("p022_names.txt","r") as f:
    for line in f:
        name=[str(v) for v in line.split(",")]
name.sort()
print(name)
tot=0
for i in range(len(name)):
    tot=tot+sum(ord(letter)-64 for letter in name[i])
print(tot)

从文件读取数据时,还会在名称中添加“和”。如果名称为COLIN,则将其另存为列表名称为““ COLIN”'。如何解决?输入文件。

"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA","CAROL","RUTH","SHARON","MICHELLE","LAURA","SARAH","KIMBERLY","DEBORAH","JESSICA","SHIRLEY","CYNTHIA","ANGELA","MELISSA","BRENDA","AMY","ANNA","REBECCA","VIRGINIA","KATHLEEN","PAMELA","MARTHA","DEBRA","AMANDA","STEPHANIE","CAROLYN","CHRISTINE","MARIE","JANET","CATHERINE","FRANCES","ANN","JOYCE","DIANE","ALICE","JULIE","HEATHER","TERESA","DORIS","GLORIA","EVELYN","JEAN","CHERYL","MILDRED","KATHERINE","JOAN","ASHLEY","JUDITH","ROSE","JANICE","KELLY","NICOLE","JUDY","CHRISTINA","KATHY","THERESA","BEVERLY","DENISE","TAMMY","IRENE","JANE","LORI","RACHEL","MARILYN","ANDREA","KATHRYN","LOUISE","SARA","ANNE","JACQUELINE","WANDA","BONNIE","JULIA","RUBY","LOIS","TINA","PHYLLIS","NORMA","PAULA","DIANA","ANNIE","LILLIAN","EMILY","ROBIN","PEGGY","CRYSTAL","GLADYS","RITA","DAWN","CONNIE","FLORENCE","TRACY","EDNA","TIFFANY","CARMEN","ROSA","CINDY","GRACE","WENDY","VICTORIA",

输出['“ MARY”','“ ...”','“ ...”',..]

4 个回复

如果要从字符串中删除这些字符,则可以执行以下操作:

s = s.replace("\\"","")

这将用空字符串替换所有的"实例。

如果只希望在字符串的末尾删除它们,则可以执行此操作

s = s.strip("\\"")

如果您想确保只在两端删除一个引号,则可以执行以下操作:

if s.startswith("\""):
    s = s[1:]
elif s.endswith("\""):
    s = s[:-1]

如果您知道字符串将始终带有引号,则可以将其简化为:

s = s[1:-1]

但是,此文件似乎是一个csv文件,最好使用csv lib解析这些文件。

您可以通过以下方式从Python字符串中删除开头和结尾字符:

name = "'COLIN'"
print(name[1:-1])

这将打印COLIN而不是'COLIN'

这是我的解决方案。 它支持以逗号分隔的多行名称,也支持文件中的多行名称。

names = []
with open('p022_names.txt') as f:
    for line in f:
        names.extend(v.lower().strip('"').strip("'") for v in line.split(","))

names.sort()

for name in names:
    tot = sum(ord(letter)-96 for letter in name)
    print("{0}: {1}".format(name, tot))
with open("p022_names.txt","r") as f:
    for line in f:
        name=[str(v) for v in line.split(",")]
name.sort()
print(name)

我假设您看到的是这样的:

['"MARY"','"PATRICIA"'...]

如果我正确理解了您的问题,则希望同时删除'和teh "字符。此处的'字符表示列表中的元素是字符串。 您的实际数据中没有任何这些。您只需要去除双引号。你不必担心单引号。你唯一的报价是"那些从文件中读取的。

另外,您可以简化

name=[str(v) for v in line.split(",")]

name = line.split(",")
1 从字符串中删除“。”和“ \\”

我的项目是使用gspread模块从Google表格捕获日志编号。 但是现在的问题是捕获的日志号采用字符串“。\\ 1300”的形式。 我只想要字符串中的数字,但无法使用下面的代码将其删除。 尝试使用.replace()函数将“ \\”替换为“”,但失败。 应该获得不带符号的字符 ...

2 如何从字符串中删除?

使用RoR 2.3.8 name有价值Tiffany & Co. Tiffany & Co. 此代码在视图中: 结果结果A: 此代码在视图中: 得到结果B: 我实际上想要Related shops with Tiffan ...

3 从字符串中删除“ @”

有一封电子邮件,要从中删除第一个“ @”符号,然后确保在第二个检查中没有多于一个。 目前,这是我的操作方式。 但是在调试之后,我发现它实际上从未从tempEmail的字符串中删除“ @”符号。 为什么? ...

2010-07-29 13:33:46 5 159   vb.net
4 从字符串中删除\\“

我在从文字中删除\\"时遇到问题。 以下是我拥有的数据示例: 我想获得此输出(通过删除\\" ): 以下是我尝试过的代码之一: 我得到的输出是: ...

2015-09-18 06:35:19 2 62   regex/ r
5 从字符串中删除 \\

我无法从 Swift 中的查询字符串中删除\\ 。 我正在使用多个选项,但它没有用,所以请帮助我。 字符串是: "INSERT OR REPLACE INTO List VALUES(NULL,\\'RS\\',\\'PRODUCTI FO\\',\\'AX[Demo]\\',\\'abc ...

7 如何删除字符串中的零

嗨,我正在从应用程序处理 c# windows,我需要删除字符串上的零,但如果下一个字符不为零,它将停止删除 样本数据 我怎样才能做到这一点希望有人帮助我 ...

9 从字符串中删除“ \\”

我有来自json的字符串。 我要删除“ \\”符号。 用它进行解析,但没有用。 调试表明它无效: 和 另外,Visual Studio显示模式@“ \\”(在这些函数中也尝试过)实际上与“ \\”不匹配。 我糊涂了。 有谁知道如何在这里创建正确的模式? ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM