简体   繁体   English

Python 读取列直到行尾

[英]Python read columns until end of line

I have to read from an csv all columns starting from the forth column.我必须从 csv 中读取从第四列开始的所有列。 For example from row[4] until the end of line.例如从 row[4] 直到行尾。 Any ideas how I achieve that?任何想法我如何实现这一目标?

for col in range(4,row):
            covid.addCell(row[col])

What have you tried so far?你试过什么了? Where is your code/attempt?您的代码/尝试在哪里?

import pandas as pd

df = pd.read_csv("XXX.csv")
df_selected = df.iloc[ :, 3: ] # from 4th column, reminder: in python we count from 0 

csv.reader returns rows as lists, so you can slice each row to get a list made of the columns from the fourth to the final column csv.reader将行作为列表返回,因此您可以对每一行进行切片以获得由第四列到最后一列的列组成的列表

import csv

with open('mycsv.csv', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        chunk = row[3:]
        # do something with chunk (which is a list)

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

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