相关疑难解决方法(0)

在Python中搜索并替换文件中的一行

我想循环遍历文本文件的内容,并在某些行上进行搜索和替换,并将结果写回文件.我可以先将整个文件加载到内存中然后再写回来,但这可能不是最好的方法.

在以下代码中,最好的方法是什么?

f = open(file)
for line in f:
    if line.contains('foo'):
        newline = line.replace('foo', 'bar')
        # how to write this newline back to the file
Run Code Online (Sandbox Code Playgroud)

python file

271
推荐指数
11
解决办法
42万
查看次数

在整个应用程序中设置 on_delete 的简单方法

我一直在使用-WdPython的说法,发现的变化我需要为了准备我升级到2.0的Django

python -Wd manage.py runserver
Run Code Online (Sandbox Code Playgroud)

主要on_delete是由于成为必需的参数。

RemovedInDjango20Warning: on_delete 将是 Django 2.0 中 ForeignKey 的必需参数。models.CASCADE如果您想保持当前的默认行为,请将其设置为on models 和 in existing migrations。

请参阅https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.ForeignKey.on_delete

是否有一个简单的正则表达式(或方法)可以用来放入on_delete我的所有外键?

regex django

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

修改文件中的一行

有没有办法在 Python 中修改文件中的单行,而无需 for 循环遍历所有行?

文件中需要修改的确切位置是未知的。

python file-io for-loop

5
推荐指数
2
解决办法
8295
查看次数

使用python替换特定行中的字符串

我正在编写一个python脚本来替换具有特定扩展名(.seq)的目录中的每个文本文件中的字符串.替换的字符串应该只来自每个文件的第二行,并且输出是一个新的子目录(称之为干净),其文件名与原始文件相同,但带有*.clean后缀.输出文件包含与原始文本完全相同的文本,但替换了字符串.我需要替换所有这些字符串:'K','Y','W','M','R','S'和'N'.

这是我在谷歌搜索后想出来的.这是非常混乱的(编程的第二周),它停止将文件复制到干净的目录而不替换任何东西.我真的很感激任何帮助.

谢谢!

import os, shutil

os.mkdir('clean')

for file in os.listdir(os.getcwd()):
    if file.find('.seq') != -1:
        shutil.copy(file, 'clean')

os.chdir('clean')

for subdir, dirs, files in os.walk(os.getcwd()):
    for file in files:
        f = open(file, 'r')
        for line in f.read():
            if line.__contains__('>'): #indicator for the first line. the first line always starts with '>'. It's a FASTA file, if you've worked with dna/protein before.
                pass
            else:
                line.replace('M', 'N')
                line.replace('K', 'N')
                line.replace('Y', 'N')
                line.replace('W', 'N')
                line.replace('R', 'N')
                line.replace('S', 'N')
Run Code Online (Sandbox Code Playgroud)

python replace

3
推荐指数
3
解决办法
4万
查看次数

有没有办法在Python中寻找特定的行?

我正在自学编程,今天的挑战是编写一个可以左右对齐文本的程序.我的主要问题是,一旦我打开文件,我不知道如何写入特定的行.是否有捷径可寻?这就是我所拥有的.我想我将需要以某种方式计算行中的字节,以便我可以f.seek到行的末尾.我觉得python已经在某个地方有这种类型的功能,但我找不到它在线搜索.有什么建议?

def align():
    name= input('what is the name of your txt file?: ')
    try:
        f=open(name + '.txt','r+')
        lines =f.readlines()
        just = input('left, right, or centre?: ')
        for i in range[,lines]:
            j = lines[i].strip('\n')
            if just == 'right':
                f.seek() #I want it to seek each line but can't figure out what variable might go here...
                f.write('{:>30}'.format(line.strip()))
            elif just == 'left':
                f.seek() #I want it to seek each line but can't figure out what variable might go here...
                f.write('{:<30}'.format(line.strip()))
                f.seek() #I want …
Run Code Online (Sandbox Code Playgroud)

python

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

标签 统计

python ×4

django ×1

file ×1

file-io ×1

for-loop ×1

regex ×1

replace ×1