Home Backend Development Python Tutorial python处理文本文件并生成指定格式的文件

python处理文本文件并生成指定格式的文件

Jun 06, 2016 am 11:32 AM
Specify format text file

import os 
import sys 
import string 
 
#以指定模式打开指定文件,获取文件句柄 
def getFileIns(filePath,model): 
  print("打开文件") 
  print(filePath) 
  print(model) 
  return open(filePath,model) 
 
#获取需要处理的文件 
def getProcFile(path): 
  return os.listdir(path) 
 
#判断是否满足某个条件,如果满足则执行 
def isTrue(outFileIns,s): 
  findStr1 = "LINE_COUNT_UPDATE   INTEGER := 0;" 
  writeStr1 = "LINE_COUNT_ERROR    INTEGER := 0;    --错误数据XX条" 
  findStr2 = "DBMS_OUTPUT.PUT_LINE('处理完毕" 
  writeStr2 = "DBMS_OUTPUT.PUT_LINE('错误数据['||LINE_COUNT_ERROR||']条.');" 
  findStr3 = "DBMS_OUTPUT.PUT_LINE('插入数据['||CUR_RESULT.INT_ID||']时发生异常...');" 
  writeStr3 = "LINE_COUNT_ERROR := LINE_COUNT_ERROR+1;" 
  findStr4 = "DBMS_OUTPUT.PUT_LINE('更新数据['||CUR_RESULT.INT_ID||']时发生异常...');" 
   
  if s.find(findStr1) != -1: 
    outFileIns.write(s) 
    outFileIns.write(writeStr1+"\n") 
  elif s.find(findStr2) != -1: 
    outFileIns.write(s) 
    outFileIns.write(writeStr2+"\n") 
  elif s.find(findStr3) != -1: 
    outFileIns.write(s) 
    outFileIns.write("\t\t\t\t"+writeStr3+"\n") 
  elif s.find(findStr4) != -1: 
    outFileIns.write(s) 
    outFileIns.write("\t\t\t\t\t"+writeStr3+"\n") 
  elif s.find("CS_OSLGIS") != -1: 
    outFileIns.write(s.replace("CS_OSLGIS","CQ_RMW")) 
  elif s.find("AND A.LONGITUDE >") != -1: 
    outFileIns.write("\t\t\tAND A.LONGITUDE IS NOT NULL\n\t\t\tAND A.LONGITUDE IS NOT NULL\n\t\t\tAND ROWNUM<2\n") 
  elif s.find(") LOOP") != -1: 
    outFileIns.write("\t\t) LOOP\n") 
  else: 
    outFileIns.write(s.replace("||')',2","||')',3")) 
 
#读取并处理文本 
def getAndProc(inFileIns,outFileIns): 
  lines = inFileIns.readlines() 
  for s in lines: 
    #print(s) 
    isTrue(outFileIns,s) 
 
if __name__=="__main__": 
   
  inFileMod = "r" 
  outFileMod = "w" 
  path = "D:\\rmsdata2gis" 
  for tmpFile in os.listdir(path): 
    inFilePath = path+"\\"+tmpFile 
    outFilePath = path+"\\BAK_"+tmpFile 
    inFileIns = getFileIns(inFilePath,inFileMod) 
    outFileIns = getFileIns(outFilePath,outFileMod) 
    getAndProc(inFileIns,outFileIns) 
    inFileIns.close() 
    outFileIns.close()
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use the fmt.Sprintf function to format a string into a specified format Use the fmt.Sprintf function to format a string into a specified format Jul 25, 2023 pm 02:05 PM

Use the fmt.Sprintf function to format a string into a specified format. In the Go language, the fmt.Sprintf function is a very practical function that can format a string into a specified format. It has a wide range of uses and can be used to build complex strings, including printing logs, generating error messages, etc. Let's look at a simple example below. Suppose we have a structure of student information, including name, age and score. Now we need to format this information into a string so it can be printed

How to read text files in html How to read text files in html Mar 26, 2024 pm 04:07 PM

HTML itself cannot read text files directly, but this functionality can be achieved through back-end programming languages ​​(such as PHP, Python, Java) or front-end JavaScript technology. The backend method uses PHP's file_get_contents() function to read the content from the text file and embed it into the HTML page. The front-end JavaScript method uses the Fetch API to send a GET request to a text file on the server, then parses the response content and displays it in an HTML page.

Best practices for reading text files with HTML Best practices for reading text files with HTML Apr 09, 2024 pm 03:45 PM

Text files can be read via HTML using the FileReader API. Best practices include filtering file types using the accept attribute, selecting multiple files using the multiple attribute, and reading files through the onchange event handler. A practical case demonstrates how to read a text file and display its content, using the readAsText() method of FileReader to load the file content into a variable.

What is the extension of text file What is the extension of text file Aug 22, 2022 pm 01:59 PM

The extension of text files is "txt". Text files are files with the TXT suffix and contain very little format information. The ".txt" format is not clearly defined. It usually refers to those formats that can be accepted by the system terminal or a simple text editor; any program that can read text can read files with the ".txt" extension. files, therefore, are generally considered to be universal and cross-platform.

In C language, what are text files and binary files? In C language, what are text files and binary files? Sep 08, 2023 pm 04:37 PM

A file is a collection of records (or) a place on a hard disk where data is permanently stored. There are two types of file languages ​​in File Type C as follows - Text file Binary file Text file It contains letters and numbers that are easily understandable by humans. Errors in text files can be eliminated under the following conditions: In text files, text and characters are stored one character per byte. For example the integer value 4567 will occupy 2 bytes in memory but 5 bytes in the text file. Data formats are usually row-oriented. Here, each line is a separate command. Binary file contains 1's and 0's and is easily understood by computers. Errors in binary files can corrupt the file and are difficult to detect. In binary file, integer value 1245 will occupy 2 bytes in memory and file

How to read text file from HTML How to read text file from HTML Apr 09, 2024 pm 01:48 PM

HTML cannot read text files directly. It can be implemented using FileReaderAPI through JavaScript: 1. Get the file input element; 2. Listen to the file selection event; 3. Create a FileReader object; 4. Listen to the loading completion event; 5. Get the file content; 6. Process the read in the event handler The text retrieved.

C++ program to print values ​​in specified format C++ program to print values ​​in specified format Sep 03, 2023 pm 12:33 PM

Suppose we have three double values. We need to format and print them as follows. We need to print the integer part of the first value in lowercase hexadecimal format. We need to print the second value to two decimal places and prepend it with a sign to show whether it is positive or negative. The second value to be printed must be right-justified, 15 characters long, and underlined in unused positions on the left. We need to print the third value in scientific notation to nine decimal places. So if the input is 256.367, 5783.489, 12.5643295643, the output will be 0x100_______+5783.491.256432956E+01 To solve this problem

How to copy odd lines of a text file into another file using Python How to copy odd lines of a text file into another file using Python Sep 14, 2023 pm 08:29 PM

In this article, we will show you how to copy odd-numbered lines of a text file to another text file using Python. Let's say we get a text file called TextFile.txt that contains some random text. We just copy all the odd lines of one text file into another text file and print them. TextFile.txtGoodMorningThisistheTutorialsPointsampleFileConsistingofSpecificsourcecodesinPython,Seaborn,ScalaSummaryandExplanationWelc

See all articles