Python 如何将 csv 文件导入数据数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46614526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:43:51  来源:igfitidea点击:

How to import a csv-file into a data array?

pythoncsvpython-2.x

提问by GFL

I have a line of code in a script that imports data from a text file with lots of spaces between values into an array for use later.

我在脚本中有一行代码,该脚本将值之间有大量空格的文本文件中的数据导入到数组中,以备后用。

textfile = open('file.txt')
data = []
for line in textfile:
    row_data = line.strip("\n").split()
    for i, item in enumerate(row_data):
        try:
            row_data[i] = float(item)
        except ValueError:
            pass
    data.append(row_data)

I need to change this from a text file to a csv file. I don't want to just change this text to split on commas (since some values can have commas if they're in quotes). Luckily I saw there is a csv library I can import that can handle this.

我需要将其从文本文件更改为 csv 文件。我不想只是更改此文本以在逗号上拆分(因为如果某些值在引号中,则它们可以包含逗号)。幸运的是,我看到有一个可以导入的 csv 库可以处理这个问题。

import csv
with open('file.csv', 'rb') as csvfile:
    ???

How can I load the csv file into the data array?

如何将 csv 文件加载到数据数组中?

If it makes a difference, this is how the data will be used:

如果它有所作为,这就是数据的使用方式:

row = 0
for row_data in (data):
    worksheet.write_row(row, 0, row_data)
    row += 1

回答by martineau

Assuming the csv file is delimited with commas, the simplest way using the csvmodule in Python 3 would probably be:

假设 csv 文件用逗号分隔,csv在 Python 3 中使用该模块的最简单方法可能是:

import csv

with open('testfile.csv', newline='') as csvfile:
    data = list(csv.reader(csvfile))

print(data)

For Python 2, use open('testfile.csv', 'rb')to open the file.

对于 Python 2,使用open('testfile.csv', 'rb')打开文件。

回答by Humi

You can use pandas library or numpy to read the CSV file. If your file is tab-separated then use '\t' in place of comma in both sepand delimiterarguments below.

您可以使用 pandas 库或 numpy 来读取 CSV 文件。如果您的文件是制表符分隔的,则在下面的sepdelimiter参数中使用 '\t' 代替逗号。

import pandas as pd 
myFile = pd.read_csv('filepath', sep=',')

Or

或者

 import numpy as np
 myFile = np.genfromtxt('filepath', delimiter=',')