Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从django导出导入操作输出_Python_Django - Fatal编程技术网

Python 从django导出导入操作输出

Python 从django导出导入操作输出,python,django,Python,Django,我有一个django应用程序,它使用类将django导出导入的输出写入静态文件夹 我想删除输出的第一行和逗号之后的任何内容 我试过使用re,但似乎无法使其发挥作用 以下是电流输出: ipadd,活动 192.168.42.33/32,1 192.168.95.1/32,1 我希望输出为: 192.168.42.33/32 192.168.95.1/32 这是我的密码 class IPResourceExport(resources.ModelResource): class Meta:

我有一个django应用程序,它使用类将django导出导入的输出写入静态文件夹

我想删除输出的第一行和逗号之后的任何内容

我试过使用re,但似乎无法使其发挥作用

以下是电流输出:

ipadd,活动
192.168.42.33/32,1
192.168.95.1/32,1

我希望输出为:

192.168.42.33/32
192.168.95.1/32

这是我的密码

class IPResourceExport(resources.ModelResource):

class Meta:
    model = IPAddress
    fields = ('ipadd', 'active',)

def deploy_ip(request):

    queryset = IPAddress.objects.filter(active=1)
    dataset = IPResourceExport().export(queryset)

    output_path = settings.STATIC_ROOT + '/iptemp.txt'

    f = open(output_path, 'w')
    f.write(dataset.csv)
    f.flush()
    f.close()
    template_name = "block/deploy.html"
    context = {}
    return render(request, template_name, context)
更新的工作代码:

def deploy_ip(request):

    queryset = IPAddress.objects.filter(active=1)
    dataset = IPResourceExport().export(queryset)

    new_data = dataset.csv
    out_data = re.sub(',1', '', new_data.split('\n', 1)[1])

    output_path = settings.STATIC_ROOT + '/output/iptemp.txt'

    f = open(output_path, 'w')
    f.write(out_data)
    f.close()
    template_name = "block/deploy.html"
    context = {}
    return render(request, template_name, context)

我将regex和split添加到函数中,以获得所需的输出。上面更新的工作代码中的代码按预期工作。谢谢你,杰布

>> import re
>>> original = "ipadd,active\n192.168.42.33/32,1\n192.168.95.1/32"
>>> print original
ipadd,active
192.168.42.33/32,1
192.168.95.1/32,1
>>> print original.split('\n', 1)
['ipadd,active', '192.168.42.33/32,1\n192.168.95.1/32']
>>> print original.split('\n', 1)[1]
192.168.42.33/32,1
192.168.95.1/32,1
other stuff
>>> print re.sub(',1', '', original.split('\n', 1)[1])
192.168.42.33/32
192.168.95.1/32
>>>

我认为最好在字符串对象
dataset
写入文件之前对其进行操作

所以我们可以从一个字符串开始

>>> original = "ipadd,active\n192.168.42.33/32,1\n192.168.95.1/32,1\nother stuff"
>>> print original
ipadd,active
192.168.42.33/32,1
192.168.95.1/32,1
然后使用string方法
split
在出现
'\n'
的地方分割字符串,并返回包含所有片段的列表。因为我们只想将它一分为二,所以我们将
maxplit
参数设置为
1

然后我们可以只使用列表的第二部分(索引1)作为新字符串,即
dataset
,删除第一行

>>> print original.split('\n', 1)[1]
192.168.42.33/32,1
192.168.95.1/32,1
other stuff
现在,为了获得
,1'
,我们可以使用
re
模块和
sub
函数执行sed搜索和替换之类的操作。我们正在搜索
',1'
的所有实例,并在目标字符串
original.blah.blah
中将它们替换为
'
,也称为nothing。 .

>>> print original.split('\n', 1)[1]
192.168.42.33/32,1
192.168.95.1/32,1
other stuff
>> import re
>>> print re.sub(',1', '', original.split('\n', 1)[1])
192.168.42.33/32
192.168.95.1/32
other stuff