Python data is written to a CSV format file

Source: Internet
Author: User

(Just pass, basic knowledge is also the foundation)

Python reads the data and stores it in an Excel open CSV format file!

The Bs4,csv,codecs,os module needs to be used here.

Don't say much nonsense, write code directly! The important content has been commented, the remaining do not understand can be their own query, or QQ group asked me. QQ Group in the past blog!

1 #Coding:utf-82  fromBs4ImportBeautifulSoup3 ImportBS44 ImportOS5 Import Time6 ImportCSV7 ImportCodecs8 9 #reads file data from XML and stores it in CSV format--can be opened using ExcelTen defopen_file (): OneFile_folder='C:\\users\\administrator\\desktop\\file\\filename' ##文件夹位置 A     ifOs.path.isdir (file_folder): -          forFileNameinchOs.listdir (file_folder): -            #Print FileName theInfo (fileName)##读取文件名字 - definfo (fileName): -Soup = bs4. BeautifulSoup (Open ('c:/users/administrator/desktop/file/filename/'+fileName)) -A = Soup.find_all ('mxxx') +info = [] -      forIinchA: +dt=[] ADt.append (I.find ('xx'). Get_text (). Strip ()) atDt.append (I.find ('xx'). Get_text (). Strip ()) -Dt.append (I.find ('xx'). Get_text (). Strip ()) -Dt.append (I.find ('xx'). Get_text (). Strip () +'\ n') -Dt.append (I.find ('xx'). Get_text (). Strip ()) -Dt.append (I.find ('xx'). Get_text (). Strip ()) -Dt.append (Float (I.find ('xx'). Get_text (). Strip ()) + Float (i.find ('xx'). Get_text (). Strip ())) in info.append (DT) -With open ("Ex_info.csv","ab+") as CSVFile:## "ab+" to remove blank lines, also known as line break!  toCsvfile.write (codecs. BOM_UTF8)##存入表内的文字格式 +writer = Csv.writer (csvfile)#format used when depositing to a table -Writer.writerow (['Table Header','Table Header']) theWriter.writerows (Info)#Writing Tables *      $ if __name__=='__main__':Panax NotoginsengOpen_file ()

Here the main three parts, I call the test using the XML inside the data, here use the BS4 to parse the XML file.

Explain the first part:

Open the folder and get the name of the file because there are multiple files, so use the function of the OS module to open the file

1  ' C:\\users\\administrator\\desktop\\file\\filename ' # #文件夹位置 2     if Os.path.isdir (file_folder): 3          for inch Os.listdir (file_folder): 4            # Print FileName 5             # #读取文件名字

FileName is the name of the file we get.

Part II:

After getting the file name, you need to use the BS4 module to open the file because there are multiple files, so the parsing process is written to the function.

1 definfo (fileName):2Soup = bs4. BeautifulSoup (Open ('c:/users/administrator/desktop/file/filename/'+fileName))3A = Soup.find_all ('mxxx')4info = []5      forIinchA:6dt=[]7Dt.append (I.find ('xx'). Get_text (). Strip ())8Dt.append (I.find ('xx'). Get_text (). Strip ())9Dt.append (I.find ('xx'). Get_text (). Strip ())TenDt.append (I.find ('xx'). Get_text (). Strip () +'\ n') OneDt.append (I.find ('xx'). Get_text (). Strip ()) ADt.append (I.find ('xx'). Get_text (). Strip ()) -Dt.append (Float (I.find ('xx'). Get_text (). Strip ()) + Float (i.find ('xx'). Get_text (). Strip ())) -Info.append (DT)

Open the folder and take out the file, and after parsing using beautifulsoup Parsing Web page method to obtain data, ' mxxx ' and ' xx ' are the name of the tree.

Part III:

Writes data to a CSV file, where the data is in the list format and needs to be traversed.

1With open ("Ex_info.csv","ab+") as CSVFile:## "ab+" to remove blank lines, also known as line break! 2Csvfile.write (codecs. BOM_UTF8)##存入表内的文字格式3writer = Csv.writer (csvfile)#format used when depositing to a table4Writer.writerow (['Table Header','Table Header','Table Header','Table Header'])5Writer.writerows (Info)#Writing Tables

The header here should be the same as the data column we obtained above, otherwise there will be an error. The format of the write file is "W", "A +", "ab+", etc., here is used "ab+", to remove the empty line!

And we if the data is stored in 1000000000 such, in Excel is the display of e+17, all in the acquisition of data when the last side add "\ n". Finally write the table inside and open!

Python data is written to a CSV format file

Related Article

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.