txt写进python

import sys
import random
import time
import xlwt
import codecs
 
def Txt_to_Excel(wb,inputTxt,start_row,start_col):
    fr = codecs.open(inputTxt,'r')
    line_number = 0
    row_excel = start_row
    for line in fr:
        line =line.split()
        col_excel =start_col;
        wb.write(row_excel,col_excel,line)
        line_number +=1
        row_excel +=1
 
 
if __name__ == '__main__':
    wb = xlwt.Workbook(encoding = 'utf-8')
    ws1 = wb.add_sheet("AppThroughput")
    startrow =0
    dir1 = './Indicator/AppThroughput/AppThroughput-ECMP-X1--0.4-50.0ms.txt'
    startcol1 =0
    Txt_to_Excel(ws1,dir1,startrow,startcol1)
    wb.save('excel_result1.xlsx')
————————————————
版权声明:本文为CSDN博主「一只积极向上的小咸鱼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_49448331/article/details/118711282

Python是一种高级编程语言,在很多数据科学和数据处理项目中被广泛使用。将数据入文本文件Python编程中的一项基本操作,这有助于您跟踪数据并将其存储在可供以后使用的地方。 要将数据入文本文件,您可以使用Python的内置函数open()和write()。 首先,使用open()函数打开一个文本文件,并指定要入的文件名和打开方式。 再使用write()函数将数据文件。 以下是一个简单的Python示例,用于在同一目录下创建一个txt文件并将数据入它: ``` # 打开一个txt文件 file = open("data.txt", "w") # 将数据文件 file.write("这是我的第一行文本\n") file.write("这是我的第二行文本\n") file.write("这是我的第三行文本\n") # 关闭文件 file.close() ``` 使用上述代码,您可以创建一个名为“data.txt”的新文本文件,并将三行文本数据入其中。 您可以使用文本编辑器打开文件以查看入的数据。 当你需要将大量数据文件时,我们建议使用循环实现数据入。 Python通过`with open`可以简化编码模式,自动帮我们关闭文件流: ``` with open('data.txt', mode='w', encoding='utf-8') as f: for i in range(10): f.write('这是第 {} 行文本\n'.format(i+1)) ``` 最后,当你需要从txt文件读取数据时,你可以使用open()方法和read()方法: ``` with open('data.txt', mode='r', encoding='utf-8') as f: for line in f: print(line.strip()) # 处理每一行文本 ``` 上述代码会逐行读取刚才下的文件中的行文本,并将其输出到控制台中。 在Python中,将数据入文本文件是一个基本任务,掌握此技能可以方便您存储和管理数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值