小编Wou*_*ter的帖子

从非子文件夹的另一个文件夹导入 python 文件

我想要的是

\n

我正在使用 Visual Studio Code 和 Python 3.7.0,我只是尝试将另一个 Python 文件从另一个文件夹导入到我的 python 文件中

\n
\n

细节

\n

这是我的文件夹结构

\n
root/\n    dir1/\n        data.txt\n        task11.py\n        task12.py\n    dir2/\n        data.txt\n        task21.py\n        task22.py\n    Helpers/\n        FileHelper/\n            ReadHelper.py\n
Run Code Online (Sandbox Code Playgroud)\n

所以一个简短的解释:

\n
    \n
  • 我在每个“任务”文件中使用相同的函数
  • \n
  • 我没有将函数放入每个“任务”文件中,而是创建了函数存在的帮助程序文件
  • \n
  • 我想将帮助程序文件“ReadHelper.py”导入到我的任务文件中
  • \n
\n
\n

我尝试过的

\n

例如在文件task11.py中:

\n

python python-import python-3.x

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

cgi python3 编码问题

我创建了一个 cgi 脚本(使用 apache 在 localhost 上运行),它将从 textarea 加载文本,然后我将使用它。我对 \xc5\xa1、\xc5\xa5、\xc3\xa9 等字符有问题,它们无法正确显示。我尝试了很多方法。这是我的短代码的一个版本,我只是在其中寻找正确的处理方法。

\n
#!C:/Python33/python \n# -*- coding: UTF-8 -*-\n \nimport cgi\nimport cgitb\n\ncgitb.enable()\n\nform = cgi.FieldStorage()\nif form.getvalue('textcontent'):\n   text_content = form.getvalue('textcontent')\nelse:\n   text_content = ""\n\n\nprint ("Content-type:text/html")\nprint ()\nprint("<!DOCTYPE html>")\nprint ("<html>")\nprint ("<head>")\nprint("<meta charset='UTF-8'></meta>")\nprint ("</head>")\nprint ("<body>")\nprint ("<form>")\nprint ("text_area:<br />")\nprint ("<textarea name='textcontent' rows='5' cols='20'></textarea>")\nprint ("<br />")\nprint ("<input type='submit' value='submit form' />")\nprint ("</form>")\nprint("<p>")\nprint(text_content) \nprint("</p>")\nprint ("</body>")\nprint ("</html>")\n
Run Code Online (Sandbox Code Playgroud)\n

这种方式使用UTF-8,当我尝试写一些东西时,它看起来像这样(写入textarea并提交):

\n
\xc4\x8d\xc3\xadtam -> \xef\xbf\xbd\xef\xbf\xbdtam\n
Run Code Online (Sandbox Code Playgroud)\n

当我在 html 部分使用 latin-1 作为 python 编码和 utf-8 作为字符集时,它的工作原理如下:

\n
\xc4\x8dasa -> \xc4\x8dasa (correctly)\n
Run Code Online (Sandbox Code Playgroud)\n

但对于带有重音符号的字符(例如 \xc3\xa1no),它会返回错误: …

cgi character-encoding python-3.x

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

使Counter.most_common返回字典

我使用了文档中的示例:

>>> Counter('abracadabra').most_common(3)
[('a', 5), ('r', 2), ('b', 2)]
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到结果:

{ 'a': 5, 'r' :2 , 'b' :2}
Run Code Online (Sandbox Code Playgroud)

假设我们想保留Counter().most_common()代码?

python dictionary python-collections

3
推荐指数
2
解决办法
2101
查看次数