def rowfilter():
field_data = {}
try:
csv_read = csv.reader(open('sample.csv', ), delimiter=',', quotechar='|')
for row in csv_read:
for field in row[7]:
print(field)
except FileNotFoundError:
print("File not found")
rowfilter()
此代码成功运行,但我希望此代码在user_input之前打印数据。 例如如果用户输入5,那么它应该打印从1到5的所有细节,以及与之关联的所有列。
答案 0 :(得分:0)
def rowfilter(col1, col2):
try:
csv_read = csv.reader(open('items.csv'), delimiter=',', quotechar='|')
for row in csv_read:
print(row[int(cols[0]):int(cols[1])])
except FileNotFoundError:
print("File not found")
inputrows = input("Enter columns in the format: col1 col2 ")
rowfilter(inputrows.split()))
我根据你的评论编辑了这个答案,一旦csv阅读器读完它就可以将csv行视为列表。因此,此方法将打印从col1到col2的列。请注意,python是0索引。
答案 1 :(得分:0)
这几乎肯定是pandas
的工作import pandas as pd
df = pd.read_csv('sample.csv', delimiter=',', quotechar='|')
#you can get the 7th column like this
df_seven = df[df.columns[7]]
#Then slice the dataframe as you see fit
df_seven[0:5] # first five rows
df_seven[4:9] # rows 4 to 9... etc