[英]Reading specific column from a csv file natively
I want to read a specific column of the answer.csv
file named "Answer".我想读取名为“Answer”的answer.csv
文件的特定列。
My code here reads the entire thing:我的代码在这里读取了整个内容:
#Reading in the corpus
with open('answer.csv','r', encoding='utf8', errors ='ignore') as fin:
raw = fin.read().lower()
Is there a way I can do it natively in python without using pandas?有没有一种方法可以在不使用 Pandas 的情况下在 python 中本地完成?
Basically you can now split at "\\n" and then at ",":基本上你现在可以在 "\\n" 处拆分,然后在 "," 处拆分:
with open('answer.csv','r', encoding='utf8', errors ='ignore') as fin:
raw = fin.read().lower()
rows = raw.split("\n")
for i in range(len(rows)):
rows[i] = rows[i].split(";") # <--- your delimiter
column = []
for j in rows:
column.append(j[1])
print(column)
If you want to exclude the first line, you need to start with i = 1
.如果要排除第一行,则需要从i = 1
开始。 Also you could use lambda function:你也可以使用 lambda 函数:
with open('combat.csv','r', encoding='utf8', errors ='ignore') as fin:
raw = fin.read().lower()
rows = raw.split("\n")
rows = map(lambda x: x.split(";"), rows)
column = list(map(lambda x: x[1], rows))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.