将CSV转换为txt,并使用Python每隔10个值开始一行



我有一个csv文件,其中包含324行495列的值数组。每行和每列的所有值都是相同的。

我需要将这个数组拆分,以便每10个值都放在一个新行中。因此,对于324行中的每一行,将有49个具有10个值的完整列和1个具有5个值的列(495列/10个值=49个具有0个值的新行和1个带有5个值)。然后转到下一行,依此类推324行。

我遇到的麻烦如下:

  1. line.split(",")似乎什么都没做
  2. 线后的一切。split似乎也没有任何作用
  3. 我不确定我的for换行符在范围内。。。是正确的
  4. 我还没有把写输出放到文本文件中,我想应该是outFile.write(这里有东西,不确定是什么)
  5. 我把"\n"放在打印声明后面,但它只是打印出来

我是一个初级程序员。

脚本:

import string
import sys
# open csv file...in read mode
inFile= open("CSVFile", 'r')
outFile= open("TextFile.txt", 'w')

for line in inFile:
    elmCellSize = line.split(",")
    for newrow in range(0, len(elmCellSize)):
        if (newrow/10) == int(newrow/10):
            print  elmCellSize[0:10]   
outFile.close()
inFile.close()

您确实应该使用csv模块,但我可以给出一些建议。

你遇到的一个问题是,当你说print elmCellSize[0:10]时,你总是取前10个元素,而不是最近的10个元素。根据你想怎么做,你可以保留一个字符串来记住最近的10个元素。我将在下面展示一个例子,在提到一些可以用代码修复的事情之后。

首先注意,line.split(',')返回一个列表。因此,您对变量名称elmCellSize的选择有点误导。如果你说lineList = line.split(',')可能更有意义?或者,如果你说lineSize = len(line.split(','))并使用它?

此外(尽管我对Python 2.x一无所知),我认为xrange是Python 2.x的一个函数,它比range更高效,尽管它的工作方式完全相同。

不说if (newrow/10) == int(newrow/10),实际上可以说if index % 10 == 0,以检查索引是否是10的倍数。%可以被认为是"余数",因此它将给出newrow除以10的余数。(例如:5%10=5;17%10=7;30%10=0)

现在,您不想打印[0:10](它将始终打印前10个元素),而是希望从当前索引向后打印10个空格。所以你可以说print lineList[index-10:index]来打印最近的10个元素。

最后你会得到类似的东西

...
lineList = line.split(',') # Really, you should use csv reader
# Open the file to write to
with open('yourfile.ext', 'w') as f:
    # iterate through the line
    for index, value in enumerate(lineList):
        if index % 10 == 0 and index != 0:
            # Write the last 10 values to the file, separated by commas
            f.write(','.join(lineList[index-10:index]))
            # new line
            f.write('n')
            # print
            print lineList[index-10:index]

我当然不是专家,但我希望这能有所帮助!

好的,我认为这个脚本几乎可以工作。

现在的问题是,它在前49行之后停止写入outFile。它为49行设置了10列,但应该有一个只有5列的第50行,因为CSV文件中的每行都是495列。因此,当前脚本将最后10个值写入新行49次,但没有;I don’我不能多拿5块。此外,由于原始CSV文件有324行,它必须再做323次。

所以,我认为现在的问题可能是在最后一个if语句中,也许需要一个else语句,但我的elif语句没有做任何事情。我想说的是,如果列表中的第6个值是行尾字符('\n'),那么将列表中的5个值写到行尾。。。它不起作用。

感谢到目前为止所有的帮助,我很感激!

这是脚本:

import string
#import sys
#import csv
# open csv file...in read mode
inFile= open("CSVFile.csv", 'r')
outFile= open("TextFile.txt", 'w')

for line in inFile:
    lineList = line.split(',') # Really, you should use csv reader
# Open the file to write to
    with open('outFile', 'w') as outFile:
        # iterate through the line
        for index, value in enumerate(lineList):
            if index % 10 == 0 and index != 0:
                # Write the last 10 values to the file, separated by space
                outFile.write('t'.join(lineList[index-10:index]))
                # new line
                outFile.write('n')
                # print
                print lineList[index-10:index]
elif lineList[6] == 'n':
            # Write the last 5 values to the file, separated by space
                outFile.write(' '.join(lineList[index-5:index]))
                # new line
                outFile.write('n')
                # print
                print lineList[index-:index]
outFile.close()
inFile.close()

相关内容

最新更新