Python Forum
How do i read particular text from text file and update those values in MS SQL table
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do i read particular text from text file and update those values in MS SQL table
#9
import glob

def parse_line(line):
    for item in ['Emp No', 'Name', 'SAL']:
        line = line.replace(item, '')
    return [item.strip() for item in line.split(':') if item.strip()]

def process_file(file_name):
    with open(file_name) as f: # No need to specify 'r': this is the default.
        line = f.readlines()[6]
    print(line)
    return parse_line(line)
        

pattern = 'C:/Test/Python/Mag/*.txt'   
files = glob.glob(pattern)   
for file_name in files: # 'file' is a builtin type, 'name' is a less-ambiguous variable name.
    emp_no, emp_name, emp_sal = process_file(file_name)
    # here add code to write to DB
add a print as line 11 in process_file, this way you will see what the line 7 is. I guess you have other files that do not fit the format provided
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: How do i read particular text from text file and update those values in MS SQL table - by buran - Nov-15-2018, 01:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  subprocess check_output text cut off Axel_Erfurt 5 690 Feb-20-2025, 02:15 PM
Last Post: DeaD_EyE
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 27,794 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  Python - Hidden Text / Html Mail python1337 1 2,273 Feb-08-2025, 10:47 AM
Last Post: python1337
  Problems writing a large text file in python Vilius 4 943 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 985 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Get an FFMpeg pass to subprocess.PIPE to treat list as text file? haihal 2 978 Nov-21-2024, 11:48 PM
Last Post: haihal
  parsing a tree of text first the right most aligned blocks of text and so on arvindikchari 2 746 Nov-21-2024, 01:42 AM
Last Post: BashBedlam
  Paste text with caret already positioned inside a placeholder; Wehaveall 1 814 Oct-15-2024, 10:28 AM
Last Post: menator01
Photo image generation with text style Belialhun 0 626 Oct-08-2024, 01:53 PM
Last Post: Belialhun
  Read TXT file in Pandas and save to Parquet zinho 2 1,198 Sep-15-2024, 06:14 PM
Last Post: zinho

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020