Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write txt files and convert csv files to pandas conditions by Python

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "Python how to read and write txt files and convert csv files and pandas conditions". In daily operation, I believe many people have doubts about how to read and write txt files and convert csv files and pandas conditions. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for everyone to answer the questions of "how to read and write txt files and convert csv files and pandas conditions". Next, please follow the editor to study!

Q1. Enter a string from the keyboard and write the string to the file record.txt.

X=input ('Please enter what to write:')

F1 = open ('record.txt','a')

F1.write (x)

Q2. The file user.txt stores the user's id,name and age information, and programs to count the number of users aged about 20.

For example, the user.txt file format is:

1 jack 20

2 john 22

3 ceily 19

4 bill 25

Import pandas as pd

Import csv

# write to txt

F2=open ('user.txt','a')

L = ['id name age','\ n1 jack 20'\ n2 john 22'\ n3 ceily 19'\ n4 bill 25']

F2.writelines (l)

# converting txt to csv and processing

Df=pd.read_csv ('user.csv')

Print (df)

Print (sum (df.age.apply (lambda x df.age.apply 1 if abs (x Mel 20))

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report