首页 > 软件 > python程序设计,里面的txt文件没法发,评论留一下联系方式我加你,价格美丽

python程序设计,里面的txt文件没法发,评论留一下联系方式我加你,价格美丽

软件 2023-03-17

使用python编程,实现对文件夹中所有txt文件中的某一列数据都加1?

import os
path = r'C:\Users\shinelon\Desktop\新建文件夹' # 替换你的文件夹
path_result = path+"\结果"
listdir = os.listdir(path)
try:
os.mkdir(path_result)
except FileExistsError:
pass
except:
print('已经改写,若重改请删除结果文件夹')
for f_name in listdir:
path_filename = path+"\\"+f_name
print(path_filename)
with open(path_filename) as txt:
for i in txt.readlines():
a = i.split(',')
b = a[2].split('.')
c = str(int(b[0])+1) +'.'+ b[1]
d = a[0] + ',' + a[1] + ',' + c
with open(path_result+'\\'+f_name,'a') as txt_result:
txt_result.write(d)
os.startfile(path_result)

Python设计 将学生学号、姓名信息存入文本文件“student.txt”中?

arrs=[["学号","姓名"],["001","张三"],["002","李四"],["003","王五"]]

ARRS = []

f=open('student.txt','w+')

for i in range(len(arrs)):

jointsFrame = arrs[i] #每行

ARRS.append(jointsFrame)

for Ji in range(2):

strNum = str(jointsFrame[Ji])

f.write(strNum)

f.write(' ')

f.write('\n')

f.close()


while True:

temp = 0

num = None

num = input("请输入学号:")

for i in range(len(arrs)):

temp = 1

if num in arrs[i]:

print("欢迎"+arrs[i][1]+"同学")

temp = 0

break

if temp == 1:

print("学号输入错误,请重试")

temp = 0

python直接读txt(或者excel)里面的文件名,然后找到那个目录里,把他们复制出来到新的文件,求代码

1 安装xlrd模块

pipinstallxlrd

2 读取Excel

#-*-coding:utf-8-*-
importxlrd
importos,shutil
defopen_excel(file='file.xls'):
try:
data=xlrd.open_workbook(file)
returndata
exceptException,e:
printstr(e)

defmycopyfile(srcfile,dstfile):
ifnotos.path.isfile(srcfile):
print"%snotexist!"%(srcfile)
else:
fpath,fname=os.path.split(dstfile)#分离文件名和路径
ifnotos.path.exists(fpath):
os.makedirs(fpath)#创建路径
shutil.copyfile(srcfile,dstfile)#复制文件
print"copy%s->%s"%(srcfile,dstfile)
dst='D:\\temp\\'
mycopyfile(srcfile,dstfile)
file_paths=data
forpinfile_paths:
srcfile=p
dstfile=dst+p.splt('/')[-1]
mycopyfile(srcfile,dstfile)

PYTHON怎么从一堆 txt文件里面(是文件里面的内容)查找一堆关键字,然后输出包含关键字的文件名称?

  • Python Code:

fromglobimportglob
lstKwds="a/j7/9/大".split("/")
lstTxtFiles=glob(r"D:\test\*.txt")
forstrTxtFileinlstTxtFiles:
withopen(strTxtFile,"r")astxtWrapper:
strContent=txtWrapper.read()
[print(strTxtFile,"->","strKwd")forstrKwdinlstKwdsifstrKwdinstrContent]
  • 演示效果:

用python编写程序5.新建一个文本文件zen.txt, 文件内容为“Python之禅”的部分内容,具体如下?

strList=[]

line=0

f=open("zen.txt","r")

forvinf.readlines():

arr=v.split()

strList.extend(arr)

line+=1

f.close()

print('行数:',line)

print('单词:',len(strList))

#行数:4

#单词:20


标签:python 信息技术 编程 txt文件

大明白知识网 Copyright © 2020-2022 www.wangpan131.com. Some Rights Reserved. 京ICP备11019930号-18