Responsive image

哟哟哟 - 2020-01-28 19:45 [GF]
来个小游戏

>>>def PathMethod(Pathstr)
>>>    [ your code]


要求:
e.g:

>>>PathMethod('d:Document\abc')
'd:Documen/abc'

>>>PathMethod('d:360\360Document\123')
'd:360/360Document/123'

游戏规则:
输入windows路径字符串,返回posix路径字符串


灏灝龙 - 2020-01-28 19:51 [B1F]
这也算的上游戏吗
  return Pathstr.replace(r"\", "/")


alex - 2020-01-28 19:51 [B2F]
正则表达式匹配然后replace


欢乐牛牛 - 2020-01-28 19:51 [B3F]
python还停留在爬爬黄图的阶段 色欲使我进步


666 - 2020-01-28 19:53 [B4F]
    return Pathstr.replace("\\", "/")

r字符串反斜杠不能放末尾


华夏守护剑 - 2020-01-28 19:55 [B5F]
一脸懵逼。。这是什么?


xoxo - 2020-01-28 19:58 [B6F]
这是在侮辱会编程的水友吧?


哟哟哟 - 2020-01-28 19:59 [B7F]
SyntaxError: EOL while scanning string literal
game over


哟哟哟 - 2020-01-28 20:00 [B8F]
>>> PathMethod('d:Document\abc')
'd:Document\x07bc'

game over



哟哟哟 - 2020-01-28 20:03 [B9F]
这个还要字符重新编码的,不是简简单单替换就行的 上面两个大佬不是中招了吗


666 - 2020-01-28 20:03 [B10F]
输入没加r啊,那就比较麻烦了


哟哟哟 - 2020-01-28 20:11 [B11F]
游戏的难点就在这,目前知乎和cdns 没人能通过,可能是大佬不屑把


灏灝龙 - 2020-01-28 20:29 [B12F]
这样的话你这题有歧义呀,如果输入是
PathMethod('d:Document\x31')
PathMethod('d:Document\061')

PathMethod('d:Document1')
在解释器看来这都是一个东西,但我咋知道你要的是啥


ilIilIliIl - 2020-01-28 20:30 [B13F]
...


13b6c9c9 - 2020-01-28 20:33 [B14F]
\是转义符啊,不加r肯定是跟后面字符连起来判断的
就拿\123来说,怎么去判断它是大写字母的S还是原本就是\123


Angelina Jolie - 2020-01-28 20:36 [B15F]


哟哟哟 - 2020-01-28 20:48 [B16F]
这篇文章转换的已经是路径对象,
而且有内置方法可以直接转换成posixPath不用该文章这么麻烦
而我的游戏是字符串对象


666 - 2020-01-28 20:49 [B17F]
本来想暴力解决来着,但是"\a"和"\x07"的区别在转义中丢失了吧,这怎么办?


哟哟哟 - 2020-01-28 20:53 [B18F]
好像是这样,看来是的确是无解的,因为不能自动添加r字符,repr也不行


哟哟哟 - 2020-01-28 20:57 [B19F]
对,的确是这样,难为大佬了,的确是不行的,因为已经是str对象了,此游戏无解...是我的逻辑有问题,浪费大家时间了,


ilIilIliIl - 2020-01-28 20:58 [B20F]
...


ilIilIliIl - 2020-01-28 21:02 [B21F]
...


666 - 2020-01-28 21:07 [B22F]
也只有实际应用上,可以获取代码文件或者去获取命令行的原生输入内容可解了,但是实际应用上也不该出现这种问题


哟哟哟 - 2020-01-28 21:08 [B23F]
你说这算不算python的缺陷,其实办法有是有,那就是覆写str


哟哟哟 - 2020-01-28 21:10 [B24F]
其实我就想把文件拖进cmd得到路径,图个方便而已.


灏灝龙 - 2020-01-28 21:11 [B25F]
搞定,只支持你的那种调用方法

def PathMethod(path_str):
  s = inspect.stack()
  this_frame_info = s[1]
  line_called_this_method = s[1].code_context[0]
  start_idx = line_called_this_method.index("PathMethod(")
  trim_front = line_called_this_method[start_idx + len("PathMethod("):]
  end_idx = trim_front[1:].index(trim_front[0]) + 1
  trimmed_str = trim_front[1:end_idx]
  return trimmed_str.replace("\\", "/")


666 - 2020-01-28 21:14 [B26F]
那你手动加个r就完了,或者获取所在文件夹把py丢进去双击就行了


ilIilIliIl - 2020-01-28 21:18 [B27F]
...


哟哟哟 - 2020-01-28 21:32 [B28F]
>>> import inspect
>>> PathMethod('D:\123')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in PathMethod
TypeError: 'NoneType' object is not subscriptable

我是cmd执行的


哟哟哟 - 2020-01-28 21:33 [B29F]
但是这样不够cool


Angelina Jolie - 2020-01-28 21:33 [B30F]






桌面版


Powered by SP Project v1.0 © 2010-2019
Time 0.002113 second(s),query:3 Gzip enabled


Top