Quantcast
Channel: User martineau - Stack Overflow
Viewing all articles
Browse latest Browse all 44

Answer by martineau for How to import a csv-file into a data array?

$
0
0

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

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

You can specify other delimiters, such as tab characters, by specifying them when creating the csv.reader, also adding skipinitialspace=True to csv.reader call if there are multiple space symbols between columns:

    data = list(csv.reader(csvfile, delimiter='\t'))

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


Viewing all articles
Browse latest Browse all 44

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>