読者です 読者をやめる 読者になる 読者になる

txtファイルの処理(データ更新、修正)JAVA,Python

①旧社員マスタ(olddata.txt)

1010 山本孝太郎      00128000
1011 田所勉          00158900
1020 山本貴宏        00235800
1021 吉田博美        00189000
1022 白石昌弘        00178900
1024 菊地英明        00167800
1025 坂本栄一        00189000
  ②基本給変更データ(changedata.txt)

1010 00138000
1012 00162000
1020 00245800
1021 00195000
1023 00186000
1024 00179800

Java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> number=new ArrayList<>();
        ArrayList<String> name=new ArrayList<>();
        ArrayList<String> payment=new ArrayList<>();
        HashMap<String,String> newpayment=new HashMap<>();
        HashMap<String, String> numname=new HashMap<String, String>();
        HashMap<String, String> numpayment=new HashMap<String, String>();
        ArrayList<String> newnumber=new ArrayList<>();
        try{
            File oldfile=new File("E://programtrain//olddata.txt");
            BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(oldfile),"Unicode"));
            String line;

            while((line=reader.readLine())!=null){
                String words[]=line.split("[\\s]+");//空白が何個でも分ける
                number.add(words[0]);
                name.add(words[1]);
                payment.add(words[2]);
                numname.put(words[0], words[1]);
                numpayment.put(words[0], words[2]);
            }
            
            
        }catch(FileNotFoundException e){
            System.out.println(e);
        }catch(IOException e){
            System.out.println(e);
        }
        
        try{
            File newfile=new File("E://programtrain//changedata.txt");
            BufferedReader newreader=new BufferedReader(new InputStreamReader(new FileInputStream(newfile),"Unicode"));
            String line;
            while((line=newreader.readLine())!=null){
                String words[]=line.split("[\\s]+");
                newpayment.put(words[0], words[1]);
                    newnumber.add(words[0]);
                
                
            }
            
        }catch(FileNotFoundException e){
            System.out.println(e);
        }catch(IOException e){
            System.out.println(e);
        }
        
        System.out.println("        社員マスタ更新");
        System.out.println("番号"+" "+"氏名"+"        "+"旧基本給"+" "+"新基本給");
        int numberlength=number.size();
        
        for(int i=0;i<number.size();i++){
            if(newnumber.contains(number.get(i))==false){
                newnumber.add(number.get(i));
            }
        }
        Collections.sort(newnumber);

            
        for(int i=0;i<newnumber.size();i++){
            if(numname.containsKey(newnumber.get(i))){
                if(newpayment.containsKey(newnumber.get(i))){
                    System.out.println(newnumber.get(i)+" "+numname.get(newnumber.get(i))+" "+numpayment.get(newnumber.get(i))+" "+newpayment.get(newnumber.get(i))+" "+"更新");
                }else{
                    System.out.println(newnumber.get(i)+" "+numname.get(newnumber.get(i))+" "+numpayment.get(newnumber.get(i))+" "+numpayment.get(newnumber.get(i)));
                }
            }else{
                System.out.println(newnumber.get(i)+" "+numname.get(newnumber.get(i))+" "+numpayment.get(newnumber.get(i))+" "+newpayment.get(newnumber.get(i))+" "+"更新");
            }
        }

    }
    
    public static void loadFile(String filename){
        
    }
    

}

Python

from  texttable import Texttable
numName={}
numPay={}
newnumPay={}
newnumName={}
f=open("olddata.txt","r",encoding="utf-8-sig")
lines=f.readlines()
for line in lines:
    wordlist=line.split(" ")
    newwordlist=[]
    for word in wordlist:
        if(word!=""):
            newwordlist.append(word.strip())
    numName.update({newwordlist[0]:newwordlist[1]})
    numPay.update({newwordlist[0]:newwordlist[2]})
cf=open("changedata.txt","r",encoding="utf-8-sig")
clines=cf.readlines()
for line in clines:
    wordlist=line.split(" ")
    newnumPay.update({wordlist[0].strip():wordlist[1].strip()})
numPay.update(newnumPay)
snumPay=sorted(numPay.items())
table=Texttable()
header=['番号','氏名','旧基本給','新基本給']
table.header(header)
for tp in snumPay:
    if tp[0] in numName:
        table.add_row([tp[0],numName[tp[0]],numPay[tp[0]],tp[1]])
    else:
        table.add_row([tp[0],"NULL","NULL",tp[1]])
print(table.draw())
cf.close()