[英]How can I save data in a csv file to an array?
I am trying to read a csv file using the module csv.我正在尝试使用模块 csv 读取 csv 文件。 Here is my code :这是我的代码:
import csv
with open('test.csv') as csvfile:
spamreader = csv.reader(csvfile, delimiter='|')
daymoment = [row[0] for row in csv.reader(csvfile, delimiter='|') if row != ''][1::]
day = [row[0] for row in spamreader]
print(daymoment)
print(day)
Here is my csv file :这是我的 csv 文件:
|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday
Morning|2|1|1|3|5|6|7
Afternoon|3|4|4|7|8|6|5
Evening|5|23|5|6|8|7|9
Night|6|1|4|2|6|4|7
But I have the following problem : there are no values in day
.但我有以下问题: day
中没有值。 I want, for example, ['Monday', '2', '3', '5', '6']
例如,我想要['Monday', '2', '3', '5', '6']
Could you help me please ?请问你能帮帮我吗 ?
Thank you !谢谢 !
For your CSV file, I fixed a typo on "Afternoon" and added a new column title based on your code, 'daymoment'.对于您的 CSV 文件,我修正了“下午”的拼写错误,并根据您的代码“白天”添加了一个新的列标题。
Daymoment|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday
Morning|2|1|1|3|5|6|7
Afternoon|3|4|4|7|8|6|5
Evening|5|23|5|6|8|7|9
Night|6|1|4|2|6|4|7
You can read in the csv file, convert it to a pandas dataframe, and read the 'monday' column to get the data out like this:您可以读取 csv 文件,将其转换为 Pandas 数据框,然后读取“星期一”列以获取数据,如下所示:
import csv
import pandasql as ps
import pandas as pd
with open('test.csv') as csvfile:
spamreader = csv.reader(csvfile, delimiter='|')
mydata = pd.DataFrame([x for x in spamreader])
mydata.columns = mydata.iloc[0]
mydata = mydata[1:]
print(mydata['Monday'])
1 2
2 3
3 5
4 6
If I'm understanding correctly, you may want to select individual days from your data and grab their values in a different structure.如果我理解正确,您可能希望从数据中选择个别日期并以不同的结构获取它们的值。 This would require data transformation using sql (imported above), like so:这将需要使用 sql(上面导入的)进行数据转换,如下所示:
transform_sql = """
select
Daymoment,
'Monday' as Day,
Monday as Daycount
from mydata
union all
select
Daymoment,
'Tuesday' as Day,
Tuesday as Daycount
from mydata
union all
select
Daymoment,
'Wednesday' as Day,
Wednesday as Daycount
from mydata
union all
select
Daymoment,
'Thursday' as Day,
Thursday as Daycount
from mydata
union all
select
Daymoment,
'Friday' as Day,
Friday as Daycount
from mydata
union all
select
Daymoment,
'Saturday' as Day,
Saturday as Daycount
from mydata
union all
select
Daymoment,
'Sunday' as Day,
Sunday as Daycount
from mydata
"""
print(ps.sqldf(transform_sql))
Daymoment Day Daycount
0 Morning Monday 2
1 Afternoon Monday 3
2 Evening Monday 5
3 Night Monday 6
4 Morning Tuesday 1
5 Afternoon Tuesday 4
6 Evening Tuesday 23
7 Night Tuesday 1
8 Morning Wednesday 1
9 Afternoon Wednesday 4
10 Evening Wednesday 5
11 Night Wednesday 4
12 Morning Thursday 3
13 Afternoon Thursday 7
14 Evening Thursday 6
15 Night Thursday 2
16 Morning Friday 5
17 Afternoon Friday 8
18 Evening Friday 8
19 Night Friday 6
20 Morning Saturday 6
21 Afternoon Saturday 6
22 Evening Saturday 7
23 Night Saturday 4
24 Morning Sunday 7
25 Afternoon Sunday 5
26 Evening Sunday 9
27 Night Sunday 7
Now you can simply select the "day" you would like from your transformed data structure.现在您可以简单地从转换后的数据结构中选择您想要的“日期”。
print(transposed_data.loc[transposed_data['Day'] == 'Monday'])
Daymoment Day Daycount
0 Morning Monday 2
1 Afternoon Monday 3
2 Evening Monday 5
3 Night Monday 6
A csv reader reads the file line by line. csv 阅读器逐行读取文件。 When the end of file is reached, the reader is exhausted and will not return anything more.当到达文件末尾时,读者已筋疲力尽,不会再返回任何内容。 If you want to process it more than once, you have to keep the rows in a data structure, for example a list:如果要多次处理它,则必须将行保留在数据结构中,例如列表:
with io.StringIO(t) as csvfile:
spamreader = list(csv.reader(csvfile, delimiter='|'))
daymoment = [row[0] for row in spamreader if row[0] != ''][1::]
day = [row[1] for row in spamreader]
print(day)
print(daymoment)
gives:给出:
['Monday', '2', '3', '5', '6']
['Afternoon', 'Evening', 'Night']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.