小编Pro*_*mer的帖子

写入文本文件时出现奇怪的空字符

我正在编写一个函数,将文本文件中的一行替换为用户输入的一行。

def file_redact(file_name: str):
    line_to_replace = int(input('Enter the line to replace (starting from 1): '))
    f = open(file_name, 'r+')
    file_data = f.readlines()
    file_data[line_to_replace - 1] = input('Write a new line:\n') + '\n'
    f.truncate(0)
    f.writelines(file_data)
    f.close()
Run Code Online (Sandbox Code Playgroud)

该函数做得很好,但是,它还在文件的开头添加了空字符。

这是我替换第一行后文件的样子:

\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00Replaced text

Sample text2

Sample text3

Sample text4

Sample text5
Run Code Online (Sandbox Code Playgroud)

任何帮助都会受到赞赏,特别是如果有更好的方法来实现这一点。

python python-3.x

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

如何将我的 Gtk::Window 移动到 gtkmm 4 屏幕上的特定位置

我对 Gtk::Window 进行了子类化,并且想将其移动到屏幕上的某个位置,但我找不到任何方法来执行此操作。\nGtk::Window::move 在 Gtk4 中被删除了吗?如果是,我怎样才能移动我的窗户?
\n我目前在窗口类的构造函数中有这个:

\n
this->set_title(_("Screenshot"));\n// this is for getting the size of the screen, and calculating the center position\nGlib::RefPtr<Glib::ObjectBase> first_monitor = Gdk::Display::get_default()->get_monitors()->get_object(0);\nGdk::Rectangle rect;\nfirst_monitor->get_rectangle(rect);\nint posx = (rect.get_width() - this->get_width()) / 2;\nthis->move(posx, -1)\nthis->show();\n
Run Code Online (Sandbox Code Playgroud)\n

编译时它给了我以下错误:

\n
src/prefswindow.cpp: In constructor \xe2\x80\x98PrefsWindow::PrefsWindow()\xe2\x80\x99:\nsrc/prefswindow.cpp:7:17: error: \xe2\x80\x98using element_type = class Glib::ObjectBase {aka class Glib::ObjectBase}\xe2\x80\x99 has no member named \xe2\x80\x98get_rectangle\xe2\x80\x99\n  first_monitor->get_rectangle(rect);\n                 ^~~~~~~~~~~~~\nsrc/prefswindow.cpp:9:8: error: \xe2\x80\x98class PrefsWindow\xe2\x80\x99 has no member named \xe2\x80\x98move\xe2\x80\x99; did you mean \xe2\x80\x98Role\xe2\x80\x99?\n  this->move(posx, -1)\n        ^~~~\n        Role\n
Run Code Online (Sandbox Code Playgroud)\n

其中类 PrefsWindow 继承自 Gtk::Window。
\n提前致谢! …

c++ gtkmm

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

如何使用自动工具为 Windows 生成配置脚本

我正在用 C++ 编写一个应用程序,并使用自动工具制作构建脚本。然后,最终用户将能够./configure && make && make install在源目录中运行。我的问题是:同样的命令可以在 Windows 上使用吗?如果没有,我如何使用 autotools 实现类似的过程?或者 autotools 无法为 Windows 生成构建脚本?如果是这样,哪个构建工具可以用于 Windows?提前致谢 !

windows autotools configure gnu-make

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

不应该像第一个一样输出第二个代码

为什么第二个代码给出的输出与第一个不同?

  1. 使用 for 循环:
my_list = [1, 2, 3, 4, 2, 6, 2, 2, 7, 3, 8, 2]
uniques = []
for item in my_list:
    if item not in uniques:
        uniques.append(item)
print(uniques)
Run Code Online (Sandbox Code Playgroud)

输出:

my_list = [1, 2, 3, 4, 2, 6, 2, 2, 7, 3, 8, 2]
uniques = []
for item in my_list:
    if item not in uniques:
        uniques.append(item)
print(uniques)
Run Code Online (Sandbox Code Playgroud)
  1. 使用列表理解:

my_list = [1, 2, 3, 4, 2, 6, 2, 2, 7, 3, 8, 2]
uniques = []
uniques …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

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

如何从装饰器的参数中删除 self 参数(如果有)

假设我有一个简单的装饰器、一个类方法和一个函数,我都用该装饰器装饰它们:

import functools

def decorator(func):
    @functools.wraps(func)
    def call(*args):
        print(args)
        func(*args)
    return call

class cls:
    @decorator
    def meth(self, a):
        pass

@decorator
def func(c):
    pass

cls().meth("a")
func("c")
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

(<__main__.cls object at 0x7f4665c50130>, 'a')
('c',)
Run Code Online (Sandbox Code Playgroud)

但我想self在方法上使用装饰器时删除参数,以便输出变为:

('a',)
('c',)
Run Code Online (Sandbox Code Playgroud)

但是,如果我只是添加args.pop(0),我将删除第一个参数,即使它不是self。如果是的话,我怎样才能只删除第一个参数self?

注意:我阅读了一些使用长代码的解决方案inspect- 但在这种伟大且易于使用的编程语言中一定有一种更短、更简单的方法?

编辑:使用@staticmethod对我来说不是一个选择,因为我需要self方法本身的参数。我只是不想让它被打印出来。

python object decorator self python-3.x

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

如何将参数传递给 make 规则?

假设我有一个使用 make 安装的 python 项目。我希望无需先安装即可运行该项目。所以我创建了这个 make 规则:

run:
    @echo "Running projectname"
    @PYTHONPATH=${PYTHONPATH}:$(abs_srcdir)/..; ./projectname
Run Code Online (Sandbox Code Playgroud)

Where./projectname运行一个简单的 python 脚本来设置和运行项目,但这在这里并不重要。像这样,我可以简单地make run在项目的根文件夹中执行来执行和测试我的应用程序,它工作得很好。现在,我想将一些命令行参数传递给程序。我试过了make run --help,它只是打印了make帮助文本。运行make run -- --help打印

run:
    @echo "Running projectname"
    @PYTHONPATH=${PYTHONPATH}:$(abs_srcdir)/..; ./projectname
Run Code Online (Sandbox Code Playgroud)

应用程序运行,在我退出后,make尝试执行一个 target --help。

现在,我如何通过例如将--help参数传递给我的应用程序make?

command-line arguments makefile sh command-line-arguments

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

Can I get all attributes that were defined in the __init__ method of a class?

Suppose I have a class like this:

class A:
    def __init__(self):
        self.a = "a"
        self.b = "b"
Run Code Online (Sandbox Code Playgroud)

How would I get a dictionary like this ?

{"a": "a", "b": "b"}
Run Code Online (Sandbox Code Playgroud)

I read this question and answers, but the dictionary in these answers always contains some "dunder" attributes as well, which I never defined in A.__init__. Will I have to use one of the solutions in the linked question and filter out the dunder attributes, or is there a …

python attributes class introspection python-3.x

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

如何使用 C++ 解析简单的类似 pango 的标记,获得类似树的节点结构?

我试图将 Pango 风格的标记解析为树状结构,叶子是文本元素,分支是带有属性的标签元素。例如:

<b>This is bold</b>, <i>italic and <span color="red">red text</span></i> !
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经得到了下面的 C++ 代码(没有类定义,我认为它们是无关紧要的),如果省略上面的部分,它可以正常工作<b></b>- 它只能解析每个级别的一个标签,但它在每个级别的多个标签上失败:

        bool parseStartTag(const std::string& s, size_t start, size_t end, std::string& tag_name, std::map<std::string, std::string>& attrs) {
            size_t tag_name_end = s.find_first_of(" \t\n", start);
            if (tag_name_end == std::string::npos or tag_name_end >= end) {
                tag_name = s.substr(start, end - start);
                return true;
            } else {
                tag_name = s.substr(start, tag_name_end - start);
            }
            start = tag_name_end;

            char quote = '\0';
            std::string attr_name = "";
            std::string attr_value = ""; …
Run Code Online (Sandbox Code Playgroud)

c++ markup parsing abstract-syntax-tree xml-parsing

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