2017-04-24 514 views
2

我想使用python将多个csv文件转换为txt,而不会丢失列对齐。python - 将csv转换为txt而不会丢失列对齐

一个CSV文件,该文件是逗号分隔,没有空格或制表符的一个例子,显示如下:

"Products" "Technologies" Region1 Region2 Region3 
Prod1  Tech1   16  0  12 
Prod2  Tech2   0  12  22 
Prod3  Tech3   22  0  36 

但是用我的剧本我结束了以下内容:

"Products" "Technologies" Region1 Region2 Region3 
Prod1 Tech1 16 0 12 
Prod2 Tech2 0 12 22 
Prod3 Tech3 22 0 36 

分隔符的选择是任意的。是否有一种相对简单的方法来实现我想要的功能,考虑到带有csv文件的表格在维度上会有所不同,并且列标题的长度会有所不同?

我用下面的Python代码:

import os 
import fileinput 
dire = "directory" 

# function for converting csv files to txt 
def csv_to_txt(names, txtfilename): 

    # remove existing txt file 
    if os.path.exists(dire + txtfilename + ".txt"): 
     os.remove(dire + txtfilename + ".txt") 

    # open the include file 
    includefile = open(dire + txtfilename + ".txt", "a") 

    # handle the csv files and convert to txt 
    with open(names, "a+") as input_file: 
     lines = [line.split(",", 2) for line in input_file.readlines()] 
     print lines 
     text_list = [" ".join(line) for line in lines] 

     for line in text_list: 
      includefile.write(line) 
    includefile.close() 


csv_to_txt(dire + "01.csv", "nameofoutputfile") 

for line in fileinput.FileInput(dire + "nameofoutputfile" + ".txt",inplace=1): 
    line = line.replace('"','') 
    line = line.replace(',',' ') 
+0

对不起,我不明白你想要什么和你现在有什么不同。 – Allen

+0

在这里,你的实际和预期的结果都是一样的。可能是你输入了错误的信息 –

+0

你用什么代码编写你的txt文件? – pingul

回答

2

CSV文件进行无格式或对齐方式,它只是数据以逗号分隔。通常情况下,表处理器的工作是渲染csv。

要将文件读入列表或字典,请使用csv标准模块。为了在漂亮打印中获得最佳效果,请使用PrettyTable或PTable叉https://pypi.python.org/pypi/PTable/0.9.0。其他工具是https://pypi.python.org/pypi/tabulate或文字表https://oneau.wordpress.com/2010/05/30/simple-formatted-tables-in-python-with-texttablehttps://pypi.python.org/pypi/beautifultable/

与PTABLE

from prettytable import from_csv 
    fp = open("myfile.csv", "r") 
    mytable = from_csv(fp) 
    fp.close() 
    mytable.border = False 
    print mytable.get_string() 

对于一些简单的表的简单snippet可能会做的一样好。就个人而言,当我不得不打印出一张没有额外麻烦的包时,我会使用一些特别的字符串格式,但是包通常更傻得到证实,支持很多选项,所以如果你要处理很多表可能是值得的努力。


Prettytable似乎是最流行的(伟大的名字)。 制表claims比最漂亮的表打印机更好的性能,节省ASCII表(现astropy.io.ascii,所以可能会有点矫枉过正,除非你是一个火箭科学家)

+0

嗨Serge, 谢谢你指点我在正确的方向。这是我正在寻找的。 –

+0

随时好运。如果您发现答案有帮助,请立即投诉或接受 – Serge

0

我已打开的.csv程序和确实(希望)你想要什么:

import tkinter as tk 
from tkinter import filedialog 
import os 
import csv as csv_package 

def fileopen(): 
    GUI=tk.Tk() 
    filepath=filedialog.askopenfilename(parent=GUI, 
             title='Select file') 
    (GUI).destroy() 
    return (filepath) 

filepath = fileopen() 
filepath = os.path.normpath(filepath) 
data = [] 
with open(filepath) as fp: 
    reader = csv_package.reader(fp, skipinitialspace=True) 
    for row in reader: 
     data.append(row) 

#make spreadsheet rows consistent length, based on longest row 
max_len_row = len(max(data,key=len)) 
for row in data: 
    if len(row) < max_len_row: 
     append_number = max_len_row - len(row) 
     for i in range(append_number): 
      row.append('') 

#create dictionary of number of columns 
longest = {} 
for times in range(len(data[0])): 
    longest [times] = 0 

#get longest entry for each column 
for sublist_index,sublist in enumerate(data): 
    for column_index,element in enumerate(sublist): 
     if longest [column_index] < len(element): 
      longest [column_index] = len(element) 

#make each column as long as the longest entry 
for sublist_index,sublist in enumerate(data): 
    for column_index,element in enumerate(sublist): 
     if len(element) < longest [column_index]: 
      amount_to_append = longest [column_index] - len(element) 
      data [sublist_index][column_index] += (' ' * amount_to_append) 

with open(filepath, 'w', newline='') as csvfile: 
    writer = csv_package.writer(csvfile) 
    for row in data: 
     writer.writerow(row) 

path, ext = os.path.splitext(filepath) 
os.rename(filepath, path + '.txt') 

前:

"Products","Technologies",Region1,Region2,Region3 
Prod1,Tech1,16,0,12 
Prod2,Tech2,0,12,22 
Prod3,Tech3,22,0,36 

后:

Products,Technologies,Region1,Region2,Region3 
Prod1 ,Tech1  ,16  ,0  ,12  
Prod2 ,Tech2  ,0  ,12  ,22  
Prod3 ,Tech3  ,22  ,0  ,36 
+0

嗨new_to_coding, 感谢您在此提供帮助。我也做了这项工作。 –

相关问题