Home>

I am having a hard time processing after reading the DAT data with f = open (file_text.get (),'r').

What i am looking for ...
1. I want to skip the first line
2. The content of DAT data is a list of such numbers.
[20395089035130095 0258387190000927700000520090015]
I want to extract the bold part from this like the first and second columns
3. Finally, I want to output as CSV

I am wandering into an unfamiliar DAT file. Thanking you in advance.

import tkinter as tk #GUI library
import tkinter.messagebox as tkm
from pathlib import Path # Library that can handle file system paths
import pandas as pd #csv Library to handle
import datetime as dt
import cx_Oracle
from tkinter import file dialog
now = dt.datetime.now () #time
time = now.strftime ('% Y% m% d-% H% M% S')
def OpenFileDlg (tbox):
   ftype = [('','*')]
   dir ='.'
   #File dialog display
   filename = filedialog.askopenfilename (filetypes = ftype, initialdir = dir)
   #Display file path in text box
   tbox.insert (0, filename)
def btn_click (): #button function
    cc = str (txt1.get ()) # Substitute text box characters for cc
    sc = str (txt2.get ())
    if not len ​​(cc) == 6:
        tkm.showerror ("Enter error", "Customer number is 6 digits")
        return
    elif not len ​​(sc) == 7:
        tkm.showerror ("Enter error", "Employee number is 7 digits")
        return
    elif file_text.get () =='':
        tkm.showerror ('error','specify data file')
        return
    else: else:
        tkm.showinfo ("Information", "Saving CSV")
    f = open (file_text.get (),'r')

root = tk.Tk () #Tk class generation
root.geometry ('500x400 + 600 + 300') #screen size + position
root.title ('input screen') # screen title

#Customer number
lbl1 = tk.Label (text ='customer number', font = (u'MS Gothic', 11,'bold'))
lbl1.place (x = 85, y = 145)
txt1 = tk.Entry (width = 30)
txt1.place (x = 160, y = 150)
btn = tk.Button (root, text ='CSV output', width = 20, font = ("Menlo", 11), command = btn_click)
btn.place (x = 155, y = 220)
#employee number
lbl2 = tk.Label (text ='employee number', font = (u'MS Gothic', 11,'bold'))
lbl2.place (x = 85, y = 175)
txt2 = tk.Entry (width = 30)
txt2.place (x = 160, y = 180)
btn1 = tk.Button (root, text = "end", width = 20, font = ("Menlo", 11), command = root.destroy)
btn1.place (x = 155, y = 250)
#Excel file dialog
label = tk.Label (root, text ='data file', font = (u'MS Gothic', 10,'bold'))
label.place (x = 100, y = 95)
file_text = tk.Entry (root, width = 40)
file_text.place (x = 100, y = 115)
fdlg_button = tk.Button (root, text ='file selection', command = lambda: OpenFileDlg (file_text))
fdlg_button.place (x = 360, y = 110)
root.mainloop () # screen display
  • Answer # 1

    You can flexibly read fixed-length files using pandas.read_fwf.

    import pandas as pd
    from io import StringIO
    # test data
    s = "" "header
    20395089035130095 025838719000092770000052009 0015 "" "
    df = pd.read_fwf (StringIO (s), skiprows = 1, header = None, colspecs = [(20,30), (47,51)], dtype = str)
    print (df)
    # 0 1
    # 0 2583871900 0015
    df.to_csv ('ret.csv', index = False)