简体   繁体   English

以“列”格式将Python列表导出为CSV

[英]Exporting Python List to CSV in “column” format

right now I have Python 3 code that takes a column of data within a CSV file and then delimits the phrases in each cell into individual words based on spaces. 现在,我有Python 3代码,该代码在CSV文件中获取一列数据,然后根据空格将每个单元格中的短语分隔成单个单词。

What I am trying to do now is export that data back into a CSV file in Column format so that it looks like this with each cell containing one word 我现在想要做的是将数据以列格式导出回CSV文件,以便每个单元格包含一个单词时看起来像这样

Keyword
Lions
Tigers
Bears
Dog
Cats

Right now, my code exports the data to a CSV file but instead of coming in as a column format, it comes through in rows. 现在,我的代码将数据导出到CSV文件,但不是以列格式出现,而是以行形式出现。

Keyword    Lions    Tigers     Bears     Dog    Cats

When I print my code in the interpreter, it comes through correctly in a column format but not when I export it! 当我在解释器中打印代码时,它以列格式正确通过,但在导出时却不正确! Here is my code 这是我的代码

with open(b'C:\Users\j\Desktop\helloworld.csv', 'r') as datafile:
    data = []
    for row in datafile:
        data.extend(item.strip() for item in row.split())
with open('test.csv', 'w') as a_file:
    for result in data:
        result = ''.join(result)
        a_file.write(result + ',')
        print(result)

Does anyone have any ideas how to transpose the data? 有谁知道如何转置数据? Thank you! 谢谢!

Since version 2.3, Python has had a builtin module for parsing csv data . 从2.3版开始,Python 内置一个用于解析csv数据的模块 Is there any reason you can't use that? 有什么理由不能使用它吗?

To give an answer, specifically on your code, though, here's what I notice: 但是,要给出答案,特别是针对您的代码,这是我注意到的:

    a_file.write(result + ',')
    print(result)

You're writing the data to the file appended with a comma, and printing it to the command line separately. 您正在将数据写入带有逗号的文件中,并将其分别打印到命令行中。 So it shows up on the command line with one output on each line, but it's getting printed to the file as: 因此它显示在命令行上,每一行都有一个输出,但是它被打印为文件:

result1,result2,result3,result4...

All CSV files are slightly different, but commas generally delimit fields, while newlines delimit rows. 所有CSV文件都略有不同,但是通常用逗号分隔字段,而用换行符分隔行。 You're getting exactly what you're outputting: one row with all of your values on it, in separate comma-delimited fields. 您将得到确切的输出:一行包含所有值,在单独的逗号分隔字段中。 Try this instead: 尝试以下方法:

    a_file.write(result + '\n')

If you're in Python 3, then you can also use the print function: 如果您使用的是Python 3,则还可以使用print函数:

    print(result, file=a_file)

You can use 您可以使用

"\n"

to make it written in the next row. 将其写在下一行中。

with open(b'C:\Users\j\Desktop\helloworld.csv', 'r') as datafile:
  data = []
  for row in datafile:
    data.extend(item.strip() for item in row.split())
with open('test.csv', 'w') as a_file:
  for result in data:
    result = ''.join(result)
    a_file.write(result + '\n')  <--
    print(result)

I don't know what result looks like, but I would say that you need to specify that you want to go to next line : 我不知道result是什么样子,但是我要说的是,您需要指定要转到下一行:

with open('test.csv', 'w') as a_file:
    for result in data:
        result = ''.join(result)
        a_file.write(result + ',') # if you want to keep the coma, or replace ',' by '\n'
        a_file.write("\n")
        print(result)

However, it's weird you don't get coma in your output, hope this helps anyway. 但是,奇怪的是您的输出中没有出现昏迷,希望无论如何这都会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM