• «
  • 1
  • 2
  • »
  • Pages: 1/2     Go

none.gif

哟哟哟

python还可以的朋友进来!

来个小游戏

>>>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路径字符串

244339.jpg

灏灝龙

B1F  2020-01-28 19:51
(喵呜~~)
这也算的上游戏吗
  return Pathstr.replace(r"\", "/")

a3.gif

alex

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

236065.jpg

欢乐牛牛

B3F  2020-01-28 19:51
(嘻嘻 你说就是你对 我说就是我对)
python还停留在爬爬黄图的阶段 色欲使我进步

silent-sun-f913.s13.workers.dev

666

    return Pathstr.replace("\\", "/")

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

0.gif

华夏守护剑

一脸懵逼。。这是什么?

none.gif

xoxo

B6F  2020-01-28 19:58
(N1+Python = 失业?!)
这是在侮辱会编程的水友吧?

none.gif

哟哟哟

回 1楼(灏灝龙) 的帖子

SyntaxError: EOL while scanning string literal
game over

none.gif

哟哟哟

回 4楼(666) 的帖子

>>> PathMethod('d:Document\abc')
'd:Document\x07bc'

game over


none.gif

哟哟哟

回 6楼(xoxo) 的帖子

这个还要字符重新编码的,不是简简单单替换就行的 上面两个大佬不是中招了吗

silent-sun-f913.s13.workers.dev

666

回 8楼(哟哟哟) 的帖子

输入没加r啊,那就比较麻烦了

none.gif

哟哟哟

回 10楼(666) 的帖子

游戏的难点就在这,目前知乎和cdns 没人能通过,可能是大佬不屑把

244339.jpg

灏灝龙

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

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

765323.jpg

ilIilIliIl

...

51234492_tamamo.gif

13b6c9c9

B14F  2020-01-28 20:33
(头像 https://chan.sankakucomplex.com/post/show/21150659)
\是转义符啊,不加r肯定是跟后面字符连起来判断的
就拿\123来说,怎么去判断它是大写字母的S还是原本就是\123

none.gif

Angelina Jolie

B15F  2020-01-28 20:36
(珍妮--[sell=0][/sell])

none.gif

哟哟哟

回 15楼(Angelina Jolie) 的帖子

这篇文章转换的已经是路径对象,
而且有内置方法可以直接转换成posixPath不用该文章这么麻烦
而我的游戏是字符串对象

silent-sun-f913.s13.workers.dev

666

本来想暴力解决来着,但是"\a"和"\x07"的区别在转义中丢失了吧,这怎么办?

none.gif

哟哟哟

回 12楼(灏灝龙) 的帖子

好像是这样,看来是的确是无解的,因为不能自动添加r字符,repr也不行

none.gif

哟哟哟

回 17楼(666) 的帖子

对,的确是这样,难为大佬了,的确是不行的,因为已经是str对象了,此游戏无解...是我的逻辑有问题,浪费大家时间了,

765323.jpg

ilIilIliIl

...

765323.jpg

ilIilIliIl

...

silent-sun-f913.s13.workers.dev

666

也只有实际应用上,可以获取代码文件或者去获取命令行的原生输入内容可解了,但是实际应用上也不该出现这种问题

none.gif

哟哟哟

回 21楼(elf0824) 的帖子

你说这算不算python的缺陷,其实办法有是有,那就是覆写str

none.gif

哟哟哟

回 22楼(666) 的帖子

其实我就想把文件拖进cmd得到路径,图个方便而已.

244339.jpg

灏灝龙

B25F  2020-01-28 21:11
(喵呜~~)
搞定,只支持你的那种调用方法

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("\\", "/")

silent-sun-f913.s13.workers.dev

666

回 24楼(哟哟哟) 的帖子

那你手动加个r就完了,或者获取所在文件夹把py丢进去双击就行了

765323.jpg

ilIilIliIl

...

none.gif

哟哟哟

回 25楼(灏灝龙) 的帖子

>>> 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执行的

none.gif

哟哟哟

回 26楼(666) 的帖子

但是这样不够cool

none.gif

Angelina Jolie

B30F  2020-01-28 21:33
(珍妮--[sell=0][/sell])
  • «
  • 1
  • 2
  • »
  • Pages: 1/2     Go