Python read txt converted into Csv__python

Source: Internet
Author: User





#-*-Coding:utf-8-*-"" "Created on Fri June 15:49:06 2016 @author: User" "" Import OS #从文件中读取某一行 LINECACHE.CHECKCAC He can refresh the cache, Linecache can cache a line of information import Linecache def getfilenameandext (filename): (Filepath,tempfilena
    me) = Os.path.split (filename);
    (shotname,extension) = Os.path.splitext (TempFileName); Return shotname filelist=[] fileoutlist=[] for filename in Os.listdir (R ' D:\input '): print filename pa= ' D:\input\ %s '%filename filelist.append (PA) name=getfilenameandext (PA) name+= '. csv ' pa= ' D:\output\%s%name ' Fileou Tlist.append (PA) Print filelist print ' ****try to open file ' LineCount = Len (open (R ' D:\input\000.txt ', ' RU '). Readli NES ()) print ' This file%d lines '%linecount s1= ' maincontent= ' ' print ' ===final data===== ' for Var in range (14,linec 
    OUNT+1): Global maincontent theline = Linecache.getline (R ' D:\input\000.txt ', var) s= theline[15:13+104] s = S.replace (', ', ') # replaces all k in the string with 8 s = S.REplace (', ', ') # replaces all k in the string with 8 if var%2 = = 0:s+= (', ') s1= s else:string =s1.st RIP (' \ n ') + s+ ' \ n ' maincontent +=string print s print ' ===final data===== ' Print maincontent # open a file FO =
Open (Fileoutlist[0], "WB") Fo.write (maincontent);
 # Close Open File Fo.close ()


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.