Tôi muốn lưu danh sách Python trong tệp CSV, ví dụ tôi có một danh sách như thế này:
['hello','how','are','you']
Tôi muốn lưu nó như:
colummn,
hello,
how,
are,
you,
Tôi đã thử như sau:
myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
bad_coder
9.81620 Huy hiệu vàng39 Huy hiệu bạc64 Huy hiệu đồng20 gold badges39 silver badges64 bronze badges
Đã hỏi ngày 27 tháng 3 năm 2015 lúc 21:54Mar 27, 2015 at 21:54
2
Nếu bạn muốn tất cả các từ trên các dòng khác nhau, bạn cần đặt Deliiter thành \n
:
l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
Output:
hello
how
are
you
Nếu bạn muốn một dấu phẩy kéo dài:
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
for ele in l:
wr.writerow[[ele+","]]
Output:
hello,
how,
are,
you,
Tôi sẽ khuyên bạn chỉ cần viết các yếu tố mà không có dấu phẩy kéo dài, không có lợi thế nào để có dấu phẩy theo dõi nhưng cũng có thể gây ra vấn đề cho bạn sau này.
Đã trả lời ngày 27 tháng 3 năm 2015 lúc 22:01Mar 27, 2015 at 22:01
1
Bạn chỉ có thể chuyển cái này dưới dạng giá trị của một dict với khóa 'cột' cho hàm tạo dữ liệu và sau đó gọi to_csv
trên DF:
In [43]:
df = pd.DataFrame[{'column':['hello','how','are','you']}]
df
Out[43]:
column
0 hello
1 how
2 are
3 you
In [44]:
df.to_csv[]
Out[44]:
',column\n0,hello\n1,how\n2,are\n3,you\n'
Đã trả lời ngày 27 tháng 3 năm 2015 lúc 22:00Mar 27, 2015 at 22:00
EdchumedchumEdChum
356K195 Huy hiệu vàng778 Huy hiệu bạc550 Huy hiệu Đồng195 gold badges778 silver badges550 bronze badges
0
Chọn 120 giá trị từ dữ liệu exsiting
randss = np.random.choice [dsmodx.logss, 120, thay thế = false] # 120 có nghĩa là 120 giá trị in [randSS]
Tạo khung dữ liệu mới với 120 dữ liệu được chọn từ bộ dữ liệu dsmodx.logss
randssdf = pd.dataFrame [randSS, cột = ["log ss"]] lưu dữ liệu dưới dạng excel randssdf.to_excel ['randomss.xlsx', index = false]
Đã trả lời ngày 23 tháng 6 năm 2021 lúc 13:26Jun 23, 2021 at 13:26
Các
Làm cách nào để lưu danh sách trong Pandas CSV?
Các
Làm cách nào để lưu danh sách trong Pandas CSV?
CSV: Nhập mô -đun CSV vào Python, tạo đối tượng nhà văn CSV và viết danh sách danh sách vào tệp trong sử dụng phương thức writerows [] trên đối tượng nhà văn. Cái này là cái gì? Pandas: Nhập thư viện gấu trúc, tạo một bản dữ liệu gấu trúc và viết dataFrame vào một tệp bằng phương thức dataFrame DataFrame.CSV [Comma Separated Values] is a simple file format, used to store data in a tabular format. CSV file stores tabular data [numbers and text] in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format.
Có nhiều phương pháp khác nhau để lưu danh sách vào CSV mà chúng ta sẽ thấy trong bài viết này.
Phương pháp 1: Sử dụng mô -đun CSV
colummn,
hello,
how,
are,
you,
0 colummn,
hello,
how,
are,
you,
1colummn,
hello,
how,
are,
you,
2colummn,
hello,
how,
are,
you,
3 colummn,
hello,
how,
are,
you,
4colummn,
hello,
how,
are,
you,
5colummn,
hello,
how,
are,
you,
6colummn,
hello,
how,
are,
you,
7colummn,
hello,
how,
are,
you,
6colummn,
hello,
how,
are,
you,
9colummn,
hello,
how,
are,
you,
6myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
1myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
2myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
3colummn,
hello,
how,
are,
you,
3 myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
5myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
6colummn,
hello,
how,
are,
you,
6myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
8colummn,
hello,
how,
are,
you,
6l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
0colummn,
hello,
how,
are,
you,
6l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
2l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
3l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
4colummn,
hello,
how,
are,
you,
4l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
6colummn,
hello,
how,
are,
you,
6myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
8colummn,
hello,
how,
are,
you,
6l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
0colummn,
hello,
how,
are,
you,
6hello
how
are
you
2l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
3l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
4colummn,
hello,
how,
are,
you,
4hello
how
are
you
6colummn,
hello,
how,
are,
you,
6hello
how
are
you
8colummn,
hello,
how,
are,
you,
6l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
0colummn,
hello,
how,
are,
you,
6with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
for ele in l:
wr.writerow[[ele+","]]
2l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
3l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
4colummn,
hello,
how,
are,
you,
4with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
for ele in l:
wr.writerow[[ele+","]]
6colummn,
hello,
how,
are,
you,
6with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
for ele in l:
wr.writerow[[ele+","]]
8colummn,
hello,
how,
are,
you,
6hello,
how,
are,
you,
0colummn,
hello,
how,
are,
you,
6hello,
how,
are,
you,
2l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
3l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
4colummn,
hello,
how,
are,
you,
4hello,
how,
are,
you,
6colummn,
hello,
how,
are,
you,
6hello,
how,
are,
you,
8colummn,
hello,
how,
are,
you,
6In [43]:
df = pd.DataFrame[{'column':['hello','how','are','you']}]
df
Out[43]:
column
0 hello
1 how
2 are
3 you
In [44]:
df.to_csv[]
Out[44]:
',column\n0,hello\n1,how\n2,are\n3,you\n'
0colummn,
hello,
how,
are,
you,
6In [43]:
df = pd.DataFrame[{'column':['hello','how','are','you']}]
df
Out[43]:
column
0 hello
1 how
2 are
3 you
In [44]:
df.to_csv[]
Out[44]:
',column\n0,hello\n1,how\n2,are\n3,you\n'
2l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
3Các
\n
4\n
5\n
6\n
7
colummn,
hello,
how,
are,
you,
6\n
9to_csv
0to_csv
1to_csv
2
colummn,
hello,
how,
are,
you,
3 to_csv
4to_csv
1to_csv
6
to_csv
1to_csv
8
Output:
Phương pháp 2: Sử dụng gấu trúc
colummn,
hello,
how,
are,
you,
0 colummn,
hello,
how,
are,
you,
00colummn,
hello,
how,
are,
you,
01colummn,
hello,
how,
are,
you,
3 colummn,
hello,
how,
are,
you,
4colummn,
hello,
how,
are,
you,
04colummn,
hello,
how,
are,
you,
6colummn,
hello,
how,
are,
you,
06colummn,
hello,
how,
are,
you,
6colummn,
hello,
how,
are,
you,
08colummn,
hello,
how,
are,
you,
6colummn,
hello,
how,
are,
you,
10myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
2colummn,
hello,
how,
are,
you,
12colummn,
hello,
how,
are,
you,
3 colummn,
hello,
how,
are,
you,
4colummn,
hello,
how,
are,
you,
15colummn,
hello,
how,
are,
you,
6colummn,
hello,
how,
are,
you,
17colummn,
hello,
how,
are,
you,
6colummn,
hello,
how,
are,
you,
19colummn,
hello,
how,
are,
you,
6colummn,
hello,
how,
are,
you,
15myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
2Các
colummn,
hello,
how,
are,
you,
34 colummn,
hello,
how,
are,
you,
3 colummn,
hello,
how,
are,
you,
36colummn,
hello,
how,
are,
you,
37colummn,
hello,
how,
are,
you,
38colummn,
hello,
how,
are,
you,
39colummn,
hello,
how,
are,
you,
40colummn,
hello,
how,
are,
you,
41colummn,
hello,
how,
are,
you,
42colummn,
hello,
how,
are,
you,
43colummn,
hello,
how,
are,
you,
3 colummn,
hello,
how,
are,
you,
45colummn,
hello,
how,
are,
you,
34colummn,
hello,
how,
are,
you,
47colummn,
hello,
how,
are,
you,
48colummn,
hello,
how,
are,
you,
49colummn,
hello,
how,
are,
you,
47Output:
Phương pháp 3: Sử dụng Numpy
colummn,
hello,
how,
are,
you,
0 colummn,
hello,
how,
are,
you,
52myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
3colummn,
hello,
how,
are,
you,
3 myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
5myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
6colummn,
hello,
how,
are,
you,
6myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
8colummn,
hello,
how,
are,
you,
6l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
0colummn,
hello,
how,
are,
you,
6l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
2l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
3l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
4colummn,
hello,
how,
are,
you,
4l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
6colummn,
hello,
how,
are,
you,
6myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
8colummn,
hello,
how,
are,
you,
6l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
0colummn,
hello,
how,
are,
you,
6hello
how
are
you
2l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
3Các
l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
4colummn,
hello,
how,
are,
you,
4with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
for ele in l:
wr.writerow[[ele+","]]
6colummn,
hello,
how,
are,
you,
6with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
for ele in l:
wr.writerow[[ele+","]]
8colummn,
hello,
how,
are,
you,
6hello,
how,
are,
you,
0colummn,
hello,
how,
are,
you,
6hello,
how,
are,
you,
2l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
3l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
4colummn,
hello,
how,
are,
you,
4hello,
how,
are,
you,
6colummn,
hello,
how,
are,
you,
6hello,
how,
are,
you,
8colummn,
hello,
how,
are,
you,
6In [43]:
df = pd.DataFrame[{'column':['hello','how','are','you']}]
df
Out[43]:
column
0 hello
1 how
2 are
3 you
In [44]:
df.to_csv[]
Out[44]:
',column\n0,hello\n1,how\n2,are\n3,you\n'
0colummn,
hello,
how,
are,
you,
6In [43]:
df = pd.DataFrame[{'column':['hello','how','are','you']}]
df
Out[43]:
column
0 hello
1 how
2 are
3 you
In [44]:
df.to_csv[]
Out[44]:
',column\n0,hello\n1,how\n2,are\n3,you\n'
2l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
3l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
4colummn,
hello,
how,
are,
you,
4In [43]:
df = pd.DataFrame[{'column':['hello','how','are','you']}]
df
Out[43]:
column
0 hello
1 how
2 are
3 you
In [44]:
df.to_csv[]
Out[44]:
',column\n0,hello\n1,how\n2,are\n3,you\n'
6colummn,
hello,
how,
are,
you,
6In [43]:
df = pd.DataFrame[{'column':['hello','how','are','you']}]
df
Out[43]:
column
0 hello
1 how
2 are
3 you
In [44]:
df.to_csv[]
Out[44]:
',column\n0,hello\n1,how\n2,are\n3,you\n'
8colummn,
hello,
how,
are,
you,
6l = ['hello','how','are','you']
import csv
with open["out.csv","w"] as f:
wr = csv.writer[f,delimiter="\n"]
wr.writerow[l]
0colummn,
hello,
how,
are,
you,
6hello
how
are
you
2\n
3myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
14myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
15myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
16myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
17myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
18myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
17myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
20colummn,
hello,
how,
are,
you,
3myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
22myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
16myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
17myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
25colummn,
hello,
how,
are,
you,
3myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
27myfile = open['/Users/user/Projects/list.csv', 'wb']
wr = csv.writer[myfile, quoting=csv.QUOTE_ALL,'\n']
wr.writerow[pos_score]
28Output: