Python 使用csv的固定长度文本文件

Python 使用csv的固定长度文本文件,python,csv,text,Python,Csv,Text,我有一个csv文件,如下所示: 123456,456789,12345,123.45,123456 123456,456789,12345,123.45,123456 123456,456789,12345,123.45,123456 line = ''.ljust(2).join(row) 123456-----456789-----12345-----123.45-----123456 spacing = ['\t', ' ', ' ', ' ', '\r\

我有一个csv文件,如下所示:

123456,456789,12345,123.45,123456 
123456,456789,12345,123.45,123456
123456,456789,12345,123.45,123456
        line = ''.ljust(2).join(row) 
123456-----456789-----12345-----123.45-----123456 
spacing = ['\t', '  ', '  ', '  ', '\r\n']
# ... the same code from before ...
        line = ''.join([j for i in zip(row, spacing) for j in i])
# ... rest of the code ...
for i in zip(row, spacing): # the zip here equals ==> [(item1, '\t'), (item2, '  ') ...]
    for j in i: # now i == (item1, '\t')
       j # so j is just the items of each tuple
我对Python编程非常陌生,但我正在学习并发现Python非常有用。我基本上希望输出如下所示:

123456    456789    12345    123.45    123456
123456    456789    12345    123.45    123456
123456    456789    12345    123.45    123456
基本上,所有字段都是右对齐的,具有固定长度。csv文件中没有标题

以下是我迄今为止尝试过的代码,正如我所说,我对Python非常陌生:

import csv
 with open('test.csv') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
    print(', '.join(row))
    with open('test2.txt', 'wb') as f:
writer = csv.writer(f)
writer.writerows(f)

任何帮助都将不胜感激:提前感谢。

好的,您的代码有一大堆问题:

  • 你的缩进全错了。这是python的基本概念之一。如果你不明白我的意思,就去网上搜索一下,读一读
  • 打开“test2.txt”的部分位于spamreader的循环中,这意味着它会被重新打开并截断“test.csv”中的每一行
  • 您正试图使用以下行将文件写入自身:writer.writerows(f)(记住?f是您要写入的文件…)
  • 您正在使用csv.writer将行写入txt文件
  • 您希望每个项目之间有一个间距,但在代码中的任何地方都没有这样做
  • 因此,为了总结所有这些问题,这里有一个固定的示例,它与您的代码实际上并没有那么远:

    import csv
    
    res = []
    # start a loop to collect the data
    with open('test.csv') as csvfile:
        spamreader = csv.reader(csvfile, delimiter=',')
        for row in spamreader:
            line = '\t'.join(row) + '\r\n' # the \n is for linebreaks. \r is so notepad loves you too
            res.append(line)
    
    # now, outside the loop, we can do this:
    with open('test2.txt', 'wb') as f:
        f.writelines(res)
    
    编辑

    如果要控制间距,可以使用如下函数:

    123456,456789,12345,123.45,123456 
    123456,456789,12345,123.45,123456
    123456,456789,12345,123.45,123456
    
            line = ''.ljust(2).join(row) 
    
    123456-----456789-----12345-----123.45-----123456 
    
    spacing = ['\t', '  ', '  ', '  ', '\r\n']
    # ... the same code from before ...
            line = ''.join([j for i in zip(row, spacing) for j in i])
    # ... rest of the code ...
    
    for i in zip(row, spacing): # the zip here equals ==> [(item1, '\t'), (item2, '  ') ...]
        for j in i: # now i == (item1, '\t')
           j # so j is just the items of each tuple
    
    这将确保每个项目之间有2个空格。默认为空格,但如果要指定ljust将使用的内容,可以向其中添加第二个参数:

            line = ''.ljust(5, '-').join(row) 
    
    那么每一行看起来都是这样的:

    123456,456789,12345,123.45,123456 
    123456,456789,12345,123.45,123456
    123456,456789,12345,123.45,123456
    
            line = ''.ljust(2).join(row) 
    
    123456-----456789-----12345-----123.45-----123456 
    
    spacing = ['\t', '  ', '  ', '  ', '\r\n']
    # ... the same code from before ...
            line = ''.join([j for i in zip(row, spacing) for j in i])
    # ... rest of the code ...
    
    for i in zip(row, spacing): # the zip here equals ==> [(item1, '\t'), (item2, '  ') ...]
        for j in i: # now i == (item1, '\t')
           j # so j is just the items of each tuple
    
    感谢Philippe T.在评论中提到这一点

    第二次编辑

    如果希望每个列具有不同的长度,则需要预定义它。最好的方法是创建一个与csv文件列长度相同的列表,每个项都是该列的长度,最后一项是该行的结尾(这很方便,因为''join本身不这样做),然后用行压缩它。假设您希望第一列有一个选项卡,然后在其他列之间各有两个空格。那么您的代码将如下所示:

    123456,456789,12345,123.45,123456 
    123456,456789,12345,123.45,123456
    123456,456789,12345,123.45,123456
    
            line = ''.ljust(2).join(row) 
    
    123456-----456789-----12345-----123.45-----123456 
    
    spacing = ['\t', '  ', '  ', '  ', '\r\n']
    # ... the same code from before ...
            line = ''.join([j for i in zip(row, spacing) for j in i])
    # ... rest of the code ...
    
    for i in zip(row, spacing): # the zip here equals ==> [(item1, '\t'), (item2, '  ') ...]
        for j in i: # now i == (item1, '\t')
           j # so j is just the items of each tuple
    
    列表理解循环有点复杂,但请这样思考:

    123456,456789,12345,123.45,123456 
    123456,456789,12345,123.45,123456
    123456,456789,12345,123.45,123456
    
            line = ''.ljust(2).join(row) 
    
    123456-----456789-----12345-----123.45-----123456 
    
    spacing = ['\t', '  ', '  ', '  ', '\r\n']
    # ... the same code from before ...
            line = ''.join([j for i in zip(row, spacing) for j in i])
    # ... rest of the code ...
    
    for i in zip(row, spacing): # the zip here equals ==> [(item1, '\t'), (item2, '  ') ...]
        for j in i: # now i == (item1, '\t')
           j # so j is just the items of each tuple
    
    通过列表理解,这将输出:[item1',\t',item2',,…]。你把它连在一起就可以了。

    试试这个:

    import csv
    with open('data.csv') as fin, open('out.txt','w') as fout:
        data = csv.reader(fin,delimiter=',')
        resl = csv.writer(fout,delimiter='\t')
        resl.writerows(data)
    

    在制表符分隔的文件中?检查这个问题是如此。。。此外,我认为可以使用BaseStringLjust(或rjust)方法来实现固定长度,而不是使用制表法。若要将每个列定义为不同的长度,您将如何编写?