Read and Write files: reads and writes one line at a time in a large file. CSV file

Source: Internet
Author: User

Read the file:

The traditional method of reading. All read out, processed by line:

Fp=open ("./ps.txt", "R");

Alllines=fp.readlines ();

Fp.close ();

For Eachline in Alllines:

Print Eachline

The recommended read method, using a file iterator, is to read and display only one row at a time. This should be the case when reading large files:

Fp=open ("./ps.txt", "R");

For Eachline in FP:

Print Eachline




To read and write files using pandas:

Import Pandas as PD

Import NumPy as NP


Read the file and convert it into Np.array:

Traind=pd.read_csv ("Train.csv")

Trainy=np.array (Traind.iloc[:,-1])

Trainx=np.array (Traind.iloc[:,1:-1]) #drop ID and TARGET


Testd=pd.read_csv ("Test.csv")

Submitid=np.array (testd.iloc[:,0]) #ID column

Testx=np.array (testd.iloc[:,1:]) #drop ID


Write the file and convert it to PD. DataFrame:

Dataset_trainblend=np.zeros ((3, 2))

DFTRAINBLEND=PD. DataFrame (Dataset_trainblend)

Dftrainblend.to_csv ("Dftrainblend3.csv", header=["RFC", "GBC"], Index=false)

Read and Write files: reads and writes one line at a time in a large file. CSV file

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.