Python:写入CSV文件中的相同行

时间:2016-02-28 16:05:04

标签: python csv

我有一个包含多行的文本文件。我想提取某些行并将它们写入CSV文件。但是,我想将特定行写入CSV文件中的同一行。例如,我的文本文件是这样的:

Name= Sarah F
Location= Baltimore MD
Name= Bob M
Location= Sacramento CA
Name= Tom M NY
Location= Brooklyn NY
Name= Anne F
Location= Morristown NJ

我要生成的我的CSV文件将包含此人的姓名,性别,所在的城市和州:

Sarah,F,Baltimore,MD
Bob,M,Sacramento,CA
Tom,M,Brooklyn,NY
Anne,F,Morristown,NJ

当我使用csv.writerows([list])时,我会将namessexcitystate写在不同的行中:

Sarah,F
Baltimore,MD
Bob,M
Sacramento,CA
Tom,M
Brooklyn,NY
Anne,F
Morristown,NJ

当我尝试使用[name, sex] citystate附加到列表时,覆盖原始列表而不是附加。

这是我的代码:

import csv

file = open("file_to_use.txt", 'r')
csv_file = open("file_to_write.csv", 'wb')
writer = csv.writer(csv_file)

Row_lines =[]

for line in file: 

    if line.startswith("Name="):
        name_line = line.replace(" ", ",")
        name_line = name_line.strip("\n")

        Row_lines.append(name_line)

    if line.startswith("Location="):
        loc_line = line.replace(" ", ",")
        loc_line = loc_line.strip("\n")                

        Row_lines.append(loc_line)

    writer.writerows(Row_lines)

csv_file.close()

我知道我在错误的地方有一些逻辑顺序,但我似乎无法弄明白。

4 个答案:

答案 0 :(得分:0)

每次调用Row_lines.append()时,都会向列表中添加一个新项。当您调用writer.writerows(Row_lines)时,列表中的每个项都将写为单独的行。

每次遇到名称行时,都应该从该行创建一个新字符串,但不要将其添加到Row_lines列表中。每次遇到位置线时,都应将其附加到名称行字符串,创建一个完整的行,您现在可以将其添加到Row_lines列表中。

而不是在循环的每次迭代中调用writerows(),而是在编译完整的行列表后调用它一次。

import csv

file = open("file_to_use.txt", 'r')
csv_file = open("file_to_write.csv", 'wb')
writer = csv.writer(csv_file)

Row_lines =[]

for line in file: 

    if line.startswith("Name="):
        name_line = line.replace(" ", ",")
        name_line = name_line.strip("\n")

        # start building the new line
        current_line = name_line

    if line.startswith("Location="):
        loc_line = line.replace(" ", ",")
        loc_line = loc_line.strip("\n")                

        # append the extra fields to the current line
        current_line = current_line + ',' + loc_line

        # add the current line to the output list
        Row_lines.append(current_line)

# call this after you have added
# all lines, not after each one
writer.writerows(Row_lines)

csv_file.close()

答案 1 :(得分:0)

您要向Row_lines添加两个不同的行,这些行代表一行csv行,您应该为每行添加一行Row_lines

答案 2 :(得分:0)

您的任务分为两部分。首先是加入行,你可以使用zip

with open(inputfile) as propsfile:
    data = [row.split("=")[1].split() for row in propsfile]

# join two at a time
data_tuples = zip(data[::2], data[1::2])

第二是写行,你可以使用csv模块:

import csv
with open(outputfile, 'w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows([name+location for name, location in data_tuples])

现在我们有outputfile中的数据:

Sarah,F,Baltimore,MD
Bob,M,Sacramento,CA
...

答案 3 :(得分:0)

这是一个不使用任何外部库的代码。

由于你的所有行都不一定(例如" Name = Tom M NY" - NY应该不在那里),这段代码会查看"之后的2个第一个数据条目"或"位置="并忽略任何后续条目(例如" NY"在上面的示例中)。

# Opening the file to use
input_file = open(r"C:\Temp\file_to_use.txt", 'r')

# Creating an empty CSV file
output_file = open(r"C:\Temp\output.csv", 'w')

# Going through the text file, it is checking whether the line holds name or location information
# If it holds location information, all saved information so far is saved to the CSV file
for line in input_file:
    line = line.split("=")
    if line[0] == "Name":
        first_name, last_name = line[1].strip().split()[:2]
    elif line[0] == "Location":
        city, state = line[1].strip().split()[:2]
        output_file.write('%s,%s,%s,%s\n' % (first_name, last_name, city, state))

# Closes the opened files
input_file.close()
output_file.close()