简体   繁体   English

Pandas 如何将列表保存到 csv

[英]Pandas how to save a list to csv

I have a list in pandas and I want to write it to a csv file where each element is a new row.我在 pandas 中有一个列表,我想将其写入 csv 文件,其中每个元素都是一个新行。

    list3= ["France","Germany","USA"]
    with open('your_file.csv', 'w') as f:
        f.write(str(list3))

But the output is horizontal instead of vertical and if anyone knows how to get rid of the quotation marks in the output I would appreciate that too.但是 output 是水平的而不是垂直的,如果有人知道如何去掉 output 中的引号,我也会很感激。

Output with my above code: Output 与我上面的代码: 在此处输入图像描述

Output that I want:我想要的 Output: 在此处输入图像描述

This is a Python list, no pandas in sight.这是一个 Python 列表,看不到pandas

In [26]: list3= ["France","Germany","USA"]                                      

Look at what str produces:看看str产生了什么:

In [27]: str(list3)                                                             
Out[27]: "['France', 'Germany', 'USA']"

That is one string with brackets and quotes.那是一个带括号和引号的字符串。

What you want is more like:你想要的更像是:

In [28]: for word in list3: print(word)                                         
France
Germany
USA

Or writing the same to a file:或将其写入文件:

In [29]: with open('txt', 'w') as f: 
    ...:     for word in list3: 
    ...:         f.write('%s\n'%word) 
    ...:                                                                        
In [30]: cat txt                                                                
France
Germany
USA

Or with print file parameter:或使用print file参数:

In [31]: with open('txt', 'w') as f: 
    ...:     for word in list3: 
    ...:         print(word, file=f) 

or you can join the strings newlines:或者您可以加入字符串换行符:

In [33]: '\n'.join(list3)                                                       
Out[33]: 'France\nGermany\nUSA'
In [34]: with open('txt', 'w') as f: 
    ...:     print('\n'.join(list3), file=f) 

You could put the list in pandas DataFrame , but then you have to turn off columns and indices when writing the csv.您可以将列表放在pandas DataFrame中,但是在编写 csv 时必须关闭列和索引。

numpy also does it with np.savetxt('txt',list3, fmt='%s') . numpy也可以使用np.savetxt('txt',list3, fmt='%s')

Lots of ways of writing such a basic list of strings to a file.将这种基本字符串列表写入文件的方法有很多。 Some basic, some using more powerful writers.一些基本的,一些使用更强大的作家。

This can be done with the CSV module.这可以通过 CSV 模块完成。 Each row must be a list but you can generate them for the csv writer, one per item in your list每行必须是一个列表,但您可以为 csv 编写器生成它们,列表中的每个项目一个

import csv
list3= ["France","Germany","USA"]
with open('your_file.csv', 'w') as f:
    csv.writer(f).writerows([row] for row in list3)

Output Output

France
Germany
USA

You may not need the CSV module at all.您可能根本不需要 CSV 模块。 This will also write the list.这也将写入列表。 The difference is that the first example would also escape internal quotes and commas, if that's a thing in your data.不同之处在于,第一个示例也会转义内部引号和逗号,如果这是您的数据中的内容。

import csv
list3= ["France","Germany","USA"]
with open('your_file.csv', 'w') as f:
    f.write("\n".join(list3))

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

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