Python从文件中提取和排序数据

时间:2012-12-11 02:51:55

标签: python file csv formatting

我正在尝试以下列格式从大型CSV文件中提取数据,假设“x”是文本或整数形式的数据。每个分组都有一个唯一的ID,但并不总是每个分组或颜色具有相同的行数。数据用逗号分隔。

id, x
red, x
green, x
blue, x 
black, x

id, x
yellow, x
green, 
blue, x 
black, x

id, x
red, x
green, x
blue, x
black, x

id, x
red, x
green, x
blue, x

id, x
red, x
green, x
blue, x 
black, x

我想以列格式重新排列数据。 ID应该是第一列,以及用逗号分隔的任何数据。我的目标是让它读取行中的第一个单词并将其放在适当的列中。

line 0 - ID - red - green - blue - yellow - black
line 1 - x, x, x,  , x,
line 2 -  , x, x, x, x,
line 3 - x, x, x,  , x,
line 4 - x, x, x,  ,  ,
line 5 - x, x, x,  , x,

这就是我的尝试......

readfile = open("db-short.txt", "r")
datafilelines = readfile.readlines()

writefile = open("sample.csv", "w")

temp_data_list = ["",]*7
td_index = 0

for line_with_return in datafilelines:
    line = line_with_return.replace('\n','') 
    if not line == '':
        if not (line.startswith("ID") or 
                line.startswith("RED") or
                line.startswith("GREEN") or
                line.startswith("BLUE") or
                line.startswith("YELLOW") or
                line.startswith("BLACK") ):
            temp_data_list[td_index] = line
            td_index += 1

            temp_data_list[6] = line
        if (line.startswith("BLACK") or line.startswith("BLACK")):
            temp_data_list[5] = line
        if (line.startswith("YELLOW") or line.startswith("YELLOW")):
            temp_data_list[4] = line
        if (line.startswith("BLUE") or line.startswith("BLUE")):
            temp_data_list[3] = line
        if (line.startswith("GREEN") or line.startswith("GREEN")):
            temp_data_list[2] = line
        if (line.startswith("RED") or line.startswith("RED")):
            temp_data_list[1] = line
        if (line.startswith("ID") or line.find("ID") > 0):
            temp_data_list[0] = line
    if line == '':
        temp_data_str = ""
        for temp_data in temp_data_list:
            temp_data_str += temp_data + ","
        temp_data_str = temp_data_str[0:-1] + "\n"
        writefile.write(temp_data_str)

        temp_data_list = ["",]*7 
        td_index = 0

if temp_data_list[0]:
    temp_data_str = ""
    for temp_data in temp_data_list:
        temp_data_str += temp_data + ","
    temp_data_str = temp_data_str[0:-1] + "\n"
    writefile.write(temp_data_str)
readfile.close()
writefile.close()

1 个答案:

答案 0 :(得分:1)

这假定Python< 2.7(因此不会利用一个with打开多个文件,使用内置writeheaders编写标题等。请注意,为了使其正常工作,我删除了CSV中逗号之间的空格。正如@JamesHenstridge所提到的那样,绝对值得阅读csv模块,以便更有意义。

import csv

with open('testfile', 'rb') as f:
  with open('outcsv.csv', 'wb') as o:
    # Specify your field names
    fieldnames = ('id', 'red', 'green', 'blue', 'yellow', 'black')

    # Here we create a DictWriter, since your data is suited for one
    writer = csv.DictWriter(o, fieldnames=fieldnames)

    # Write the header row
    writer.writerow(dict((h, h) for h in fieldnames))

    # General idea here is to build a row until we hit a blank line,
    # at which point we write our current row and continue
    new_row = {}
    for line in f.readlines():
      # This will split the line on a comma/space combo and then
      # Strip off any commas/spaces that end a word
      row = [x.strip(', ') for x in line.strip().split(', ')]
      if not row[0]:
        writer.writerow(new_row)
        new_row = {}
      else:
        # Here we write a blank string if there is no corresponding value;
        # otherwise, write the value
        new_row[row[0]] = '' if len(row) == 1 else row[1].strip()

    # Check new_row - if not blank, it hasn't been written (so write)
    if new_row:
      writer.writerow(new_row)

使用上面的数据(抛出一些随机逗号分隔的数字),写道:

id,red,green,blue,yellow,black
x,"2,8","2,4",x,,x
x,,,"4,3",x,x
x,x,x,x,,x
x,x,x,x,,
x,x,x,x,,x