标题: [问题求助] Python把TXT转换为csv格式,转换后对数据作分列删除和增加列操作 [打印本页]
作者: superman 时间: 2021-6-5 22:35 标题: Python把TXT转换为csv格式,转换后对数据作分列删除和增加列操作
本帖最后由 superman 于 2021-7-20 23:15 编辑
刚接触python想将TXT文本数据转换为csv格式保存,然后还需要对第二列数据作分列删除URL,然后再增加一列固定列,具体情况见附件,求大佬指点,谢谢- #-*-coding:utf-8 -*-
-
- import csv
- with open('File.csv', 'w+',newline='' ) as csvfile:
- spamwriter = csv.writer(csvfile, dialect='excel')
- # 读要转换的txt文件
- with open('python测试数据.txt', 'r',encoding='utf-8') as filein:
- for line in filein:
- line_list = line.strip('\n').split(',')
- spamwriter.writerow(line_list)
复制代码
附件https://wwr.lanzoui.com/imOsppu5lub
作者: wxyz0001 时间: 2021-6-24 12:20
- import csv
- import datetime
- if __name__ == '__main__':
- read_txt = 'E:/Python/python测试数据.txt'
- write_csv = 'E:/Python/link.csv'
- today = datetime.date.today()
- with open(read_txt, 'r', encoding='utf8') as f:
- lines = f.readlines()
- for i in range(len(lines)):
- link = lines[i].split(',')[0]
- col = lines[i][lines[i].rindex('/')+1:].strip('\n')
- time = str(today.strftime('%Y%m%d')) + '-xl'
- data = [link, col, time]
- with open(write_csv, 'a', newline='') as c:
- w = csv.writer(c, dialect='excel')
- w.writerow(data)
复制代码
作者: superman 时间: 2021-7-20 23:13
回复 2# wxyz0001
感谢老师帮助
作者: superman 时间: 2021-7-22 21:37
回复 2# wxyz0001
老师您好,我还想知道如果要给这段程序加上一个进度条的话应该这样实现呢,我百度了一些案例但是一直不得要领,求指教,谢谢
作者: wxyz0001 时间: 2021-7-23 17:10
回复 4# superman - import csv
- import datetime
- from tqdm import tqdm # 安装第三方库
-
- read_txt = 'E:/news/Python/python测试数据.txt'
- write_csv = 'E:/news/Python/data.csv'
- today = datetime.date.today()
- with open(read_txt, 'r', encoding='utf8') as f:
- lines = f.readlines()
- for line in tqdm(lines):
- link = line.split(',')[0]
- col = line[line.rindex('/')+1:].strip('\n')
- time = str(today.strftime('%Y%m%d')) + '-xl'
- data = [link, col, time]
- with open(write_csv, 'a', newline='') as c:
- w = csv.writer(c, dialect='excel')
- w.writerow(data)
复制代码
作者: superman 时间: 2021-7-29 09:44
回复 5# wxyz0001
谢谢老师回复,另外还有一个问题 'with open(write_csv, 'a', newline='') as c:'这句里的'a'是追加写入,如果我改为覆盖写入'w',却只有首行有数据,数据为该文本的最后一行,前面的都被覆盖掉了,这是什么原因呢? 我希望达到的目的是,每次写都清空csv文件内的数据,然后再写入
作者: Gin_Q 时间: 2021-7-29 12:48
- #-*-coding:utf-8 -*-
-
- import csv
- import time
-
- def getLineData(constant):
- # 读要转换的txt文件
- with open('python测试数据.txt', 'r', encoding='utf-8') as filein:
- for line in filein:
- a, b = line.strip('\n').split(',')
- yield a, b[b.rfind('/')+1:], constant
-
- def saveDataToCsv(fileName):
-
- constant = time.strftime('%Y%m%d') + '-xl'
-
- with open(fileName, 'w', newline='' ) as csvfile:
- spamwriter = csv.writer(csvfile, dialect='excel')
- for line in getLineData(constant):
- spamwriter.writerow(line)
-
- if __name__ == "__main__":
-
- fileName = 'File.csv'
- saveDataToCsv(fileName)
复制代码
回复 6# superman
作者: superman 时间: 2021-7-29 13:51
回复 7# Gin_Q
您的代码运行速度非常快,感谢!
作者: superman 时间: 2021-8-3 11:19
回复 7# Gin_Q
老师您好,如果我想把结果保存为xlsx的Excel文件,首行A1,B1设置为标题行(A1="标题1",B1="标题2"),又该怎样实现呢?谢谢
作者: superman 时间: 2021-8-3 11:19
回复 5# wxyz0001
老师您好,如果我想把结果保存为xlsx的Excel文件,首行A1,B1设置为标题行(A1="标题1",B1="标题2"),又该怎样实现呢?谢谢
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |