txt文件修改

1.保存19类

path='19'
path2='源文件'
path3='1'
f = open(path, "a")
f2 = open(path2, "r")
f3 = open(path3,"a")
for i in range(5011):
    a=f2.readline()
    if a[7:9]==' 1':
        f3.write(a[0:6]+'\n')
    else:
        f.write(a[0:6]+'\n')
    print a
f.close()
f2.close()
f3.close()

2.保存10类

datapath='文件路径'
for i in range(10):
    f1 = open(datapath+'test3.txt', "a")
    f2 = open(datapath+VOC_BBOX_LABEL_NAMES[i]+'_test.txt', "r")
    for i in range(4952):
        a=f2.readline()
        if a[7:9]==' 1':
            f1.write(a[0:6]+'\n')
        print a
f1.close()
f2.close() 

3.去除重复

#coding:utf-8 
import shutil 
readDir = datapath+'源.txt'
writeDir = datapath+ "目标.txt"
lines_seen = set() 
outfile=open(writeDir,"w") 
f = open(readDir,"r") 
for line in f: 
    if line not in lines_seen: 
        outfile.write(line) 
        lines_seen.add(line) 
outfile.close() 
print "success"
f.close()

4.创建每一类的样本名txt

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import xml.etree.ElementTree as ET
VOC_BBOX_LABEL_NAMES = (
    # 'wbc',
    # 'rbc',
    'platelets'
)
data_dir='/media/chenli/G/BCCD'
files = os.listdir(data_dir+'/Annotations')
f = open('/media/chenli/G/BCCD/ImageSets/Main/'+VOC_BBOX_LABEL_NAMES+'.txt', "a")
for id_ in files:
    anno = ET.parse(os.path.join(data_dir, 'Annotations', id_))
    for obj in anno.findall('object'):

                # when in not using difficult split, and the object is
                # difficult, skipt it.
        name = obj.find('name').text.lower().strip()
                    # print name
        if name in VOC_BBOX_LABEL_NAMES:
            f.write(id_[0:16]+'\n')
            break
f.close()

5.分成train和test

#coding:utf-8 
import shutil 
writeDir= '/media/chenli/G/BCCD/ImageSets/Main/'+VOC_BBOX_LABEL_NAMES+'_all.txt'
writeDir1= '/media/chenli/G/BCCD/ImageSets/Main/'+VOC_BBOX_LABEL_NAMES+'.txt'
writeDir2= '/media/chenli/G/BCCD/ImageSets/Main/'+VOC_BBOX_LABEL_NAMES+'_test.txt'
readDir = '/media/chenli/G/BCCD/ImageSets/Main/trainval.txt'
lines_seen = set() 
outfile=open(writeDir1,"w") 
f = open(readDir,"r") 
f2 = open(writeDir,"r")
outfile2=open(writeDir2,"w")
for line in f: 
    if line not in lines_seen:  
        lines_seen.add(line) 
for line in f2:
    if line in lines_seen:
        outfile.write(line) 
    else:
        outfile2.write(line) 
outfile.close() 
outfile2.close() 
print "success"
f.close()

6.将一个文件的文本添加到另外一个文件中

f = open('1.txt', "r")
f2 = open('2.txt', "a")
f2.write(f.read())
f2.close()
f.close

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用C++中的文件输入输出流来修改txt文件中的数据。以下是一个简单的示例代码,可以实现向txt文件中写入数据和修改数据。 ```c++ #include <iostream> #include <fstream> #include <string> using namespace std; int main() { // 打开文件 ifstream file("data.txt"); // 读取文件中的内容 string content; getline(file, content); // 输出文件中的内容 cout << "文件中的内容为:" << endl; cout << content << endl; // 关闭文件 file.close(); // 修改文件中的内容 content = "Hello, world!"; // 打开文件 ofstream outfile("data.txt"); // 写入内容到文件 outfile << content; // 关闭文件 outfile.close(); // 重新打开文件 ifstream newfile("data.txt"); // 读取修改后的文件内容 string newContent; getline(newfile, newContent); // 输出修改后的文件内容 cout << "修改后的文件内容为:" << endl; cout << newContent << endl; // 关闭文件 newfile.close(); return 0; } ``` 在以上示例代码中,我们首先使用 ifstream 类型的对象打开了一个名为 "data.txt" 的文件,并将其中的内容读取到了字符串变量 content 中。然后,我们输出了这个字符串变量的值。接着,我们关闭了这个文件。 接下来,我们修改了 content 的值,并使用 ofstream 类型的对象打开了同一个文件,并将修改后的 content 写入到了这个文件中。最后,我们再次打开这个文件,并将其中的内容读取到了一个新的字符串变量 newContent 中,最终输出了修改后的文件内容。 需要注意的是,在使用文件输入输出流时,需要对文件进行打开、读取、写入和关闭等操作。同时,还需要注意文件的访问权限和路径等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值