小编Bug*_*der的帖子

输入文件名作为字符串并返回字典

我有这个功能:

def make_happiness_table("word-happiness.csv"):
   ''' make_happiness_table: string -> dict
      creates a dictionary of happiness scores from the given file '''

   return {}
Run Code Online (Sandbox Code Playgroud)

但是在尝试运行程序时我一直遇到语法错误.

File "happiness.py", line 13
def make_happiness_table("word-happiness.csv"):
                                            ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

python string filenames dictionary

1
推荐指数
1
解决办法
127
查看次数

掩盖字符串中的数字

我想更改包含数字的文本.数字的第一个和最后一个字符需要保留,其他字符应替换为a *.与文本混合的数字应保持不变.

我有这个代码:

string result = Regex.Replace(input, @"(?<=[\d]{1})[\d]*(?=[\d]{1})", 
                              m => new string('*', m.Length));
Run Code Online (Sandbox Code Playgroud)

用这个输入:

"Lorem 1 ip234sum 22 dolor 3234445 sit amet, adipiscing 1234"
Run Code Online (Sandbox Code Playgroud)

应该显示这个结果:

"Lorem 1 ip234sum 22 dolor 3*****5 sit amet, adipiscing 1**4"
Run Code Online (Sandbox Code Playgroud)

但是,目前的输出是:

"Lorem 1 ip2*4sum 22 dolor 3*****5 sit amet, adipiscing 1**4"
Run Code Online (Sandbox Code Playgroud)

如你所见,它也不正确地运作ip234sum.如何让它忽略不完全数字的字符串部分?

c# regex

1
推荐指数
1
解决办法
78
查看次数

标签 统计

c# ×1

dictionary ×1

filenames ×1

python ×1

regex ×1

string ×1