#TSV为用制表符tab分隔的文本文件。 CSV为用逗号,分隔的文本文件。
#在open的时候加上 newline=”” 这个参数,解决每隔一行会出现空白行问题。
txt>>>csv
(1)Python: 3行代码实现txt文件转csv
代码:
import numpy as np
import pandas as pd
txt = np.loadtxt('file.txt')
txtDF = pd.DataFrame(txt)
txtDF.to_csv('file.csv',index=False)
with open('data.csv','w+',encoding="utf8",newline='')as c:
writer_csv = csv.writer(c,dialect="excel")
with open("data.txt",'r',encoding='utf8')as f:
# print(f.readlines())
for line in f.readlines():
# print(line)
line_list = line.strip('\n').split() #去掉str左右端的空格并以空格分割成list
print(line_list)
hbaseRowID_list = line_list[0:3] #取前三个list中的元素
# print(hbaseRowID_list)
# hbaseRowID = line_list[0]+line_list[1]+line_list[2]
hbaseRowID = "-".join(hbaseRowID_list) #连接list
# print(hbaseRowID)
# print(type(line_list))
# print(line_list)
line_list[2] = hbaseRowID
print(line_list[2:])
writer_csv.writerow(line_list[2:])
txt>>>>>>tsv
代码:
with open('data.tsv','w+',encoding="utf8")as t:
with open("data.txt",'r',encoding='utf8')as f:
# print(f.readlines())
for line in f.readlines():
# print(line)
line_list = line.strip('\n').split() #去掉str左右端的空格并以空格分割成list
# print(line_list)
hbaseRowID_list = line_list[0:3] #取前三个list中的元素
# print(hbaseRowID_list)
# hbaseRowID = line_list[0]+line_list[1]+line_list[2]
hbaseRowID = "-".join(hbaseRowID_list) #连接list
# print(hbaseRowID)
# print(type(line_list))
# print(line_list)
line_list[2] = hbaseRowID
tsv_list = line_list[2:]
tsv_list = '\t'.join(tsv_list)
print(tsv_list)
t.write(tsv_list+'\n')
#在open的时候加上 newline=”” 这个参数,解决每隔一行会出现空白行问题。
txt>>>csv
(1)Python: 3行代码实现txt文件转csv
代码:
import numpy as np
import pandas as pd
txt = np.loadtxt('file.txt')
txtDF = pd.DataFrame(txt)
txtDF.to_csv('file.csv',index=False)
(2)
代码:
with open('data.csv','w+',encoding="utf8",newline='')as c:
writer_csv = csv.writer(c,dialect="excel")
with open("data.txt",'r',encoding='utf8')as f:
# print(f.readlines())
for line in f.readlines():
# print(line)
line_list = line.strip('\n').split() #去掉str左右端的空格并以空格分割成list
print(line_list)
hbaseRowID_list = line_list[0:3] #取前三个list中的元素
# print(hbaseRowID_list)
# hbaseRowID = line_list[0]+line_list[1]+line_list[2]
hbaseRowID = "-".join(hbaseRowID_list) #连接list
# print(hbaseRowID)
# print(type(line_list))
# print(line_list)
line_list[2] = hbaseRowID
print(line_list[2:])
writer_csv.writerow(line_list[2:])
txt>>>>>>tsv
代码:
with open('data.tsv','w+',encoding="utf8")as t:
with open("data.txt",'r',encoding='utf8')as f:
# print(f.readlines())
for line in f.readlines():
# print(line)
line_list = line.strip('\n').split() #去掉str左右端的空格并以空格分割成list
# print(line_list)
hbaseRowID_list = line_list[0:3] #取前三个list中的元素
# print(hbaseRowID_list)
# hbaseRowID = line_list[0]+line_list[1]+line_list[2]
hbaseRowID = "-".join(hbaseRowID_list) #连接list
# print(hbaseRowID)
# print(type(line_list))
# print(line_list)
line_list[2] = hbaseRowID
tsv_list = line_list[2:]
tsv_list = '\t'.join(tsv_list)
print(tsv_list)
t.write(tsv_list+'\n')