I'm new to python and now need to use "with...open...as" to import a given csv file to pycharm, here's the csv file enter image description here
The output I want is like [(0,1),(0,6),(1,7),...]
Please can someone suggest how to write?
I'm new to python and now need to use "with...open...as" to import a given csv file to pycharm, here's the csv file enter image description here
The output I want is like [(0,1),(0,6),(1,7),...]
Please can someone suggest how to write?
Try this:
from csv import reader
dataset = [] # Initialize an empty array so we can append elements into it.
with open('import-csv-as-coordinates-to-python.csv', newline='') as csvfile: # Open the csv file for reading.
csvreader = reader(csvfile, delimiter=',') # Initialize the csv reader from the file.
for rowid, row in enumerate(csvreader): # Go through every row in the list.
if rowid == 0: # This row contains the fields to the data, we can ignore it.
continue # Continue onto the next row.
dataset.append((row[1], row[2],)) # Add a tuple consisting of x, y to the list dataset.
print(dataset) # Output the dataset to make sure it worked.
You can find the documentation for the csv module here](https://docs.python.org/3/library/csv.html). This is an official built-in module.
If you'd like, you can also parse the csv yourself by calling line.split(',')
for every line
in the file.readlines()
, like this.
dataset = []
with open('import-csv-as-coordinates-to-python.csv', newline='') as csvfile:
for rowid, row in enumerate(csvfile.readlines()):
row = row.strip().split(',')
if rowid == 0:
continue
dataset.append((row[1], row[2],))
print(dataset)
This is really a duplicate, but the answer is trivial:
# cat file.csv
id,x,y
1,0,1
2,0,6
3,1,7
# python -c "import pandas as pd;df=pd.read_csv('file.csv',sep=',');print(df.head());"
id x y
0 1 0 1
1 2 0 6
2 3 1 7
# python -c "import pandas as pd;df=pd.read_csv('file.csv',sep=',',names=['x','y']);print(df[1:].values.tolist());"
[['0', '1'], ['0', '6'], ['1', '7']]
# python -c "import pandas as pd;df=pd.read_csv('file.csv',sep=',',names=['x','y']);print(list(zip(df['x'],df['y']))[1:]);"
[('0', '1'), ('0', '6'), ('1', '7')]