简体   繁体   English

我如何从Python中的csv文件开始?

[英]How can i start at a line with a csv file in python?

import glob
import csv

with open('last30daysdisabled.csv') as f:

    reader = csv.reader(f)

    for line in reader:
        print(line)

How would i go about turning this into a list? 我将如何将其转换为列表?

Use enumerate() you can have the line number as below, by default the count start is starting from 0, you can force it starting from 1: 使用enumerate()你可以有如下的行号,默认情况下计数start从0开始,你可以迫使它从1开始:

for line_number, line in enumerate(reader,start=1):
    if line_number > 3:
        print(line)

Using next you can skip the first two lines by consuming them: 使用next可以通过使用它们来跳过前两行:

reader = csv.reader(f)
next(reader)
next(reader)

for line in reader:
    print(line)

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

相关问题 如何在 python 中打开一个 csv 文件,一次读取一行,而不将整个 csv 文件加载到内存中? - How can I open a csv file in python, and read one line at a time, without loading the whole csv file in memory? 使用熊猫如何计算csv文件Python中的行出现次数 - Using pandas how I can count the line occurrences in a csv file Python 如何将CSV文件的最后一行读入我可以操作的列表(python) - How to read last line of CSV file into a list that I can manipulate (python) 如果csv文件的最后一行在Python中只有1列,我怎么不读它呢? - How can I not read the last line of a csv file if it has simply 1 column in Python? 如何在 python 中使用 CSV 文件? - How can I use a CSV file in python? 如何逐行合并csv文件python - How to merge csv file line by line python 如何使用python将csv文件中的None行删除? - How do I drop the None line in a csv file using python? 如何使用 Python 覆盖 CSV 文件中的特定行 - How do I overwrite a specific line in a CSV file with Python 如何使用 Python 将 csv 的第一行替换为 header? - How can I replace first line of a csv with a header using Python? 如果在使用python中的csv模块解析csv文件时此行中的数据不正确,如何跳过一行 - How do I skip a line if the data in this line is not correct while parsing csv file using csv module in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM