0

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?

qazwsx
  • 7
  • 4
  • 1
    Hi Luke and welcome to SO. Please recall that we assume that you're able to google and search here first, and then post something you tried. Does this answer your question? [Import CSV file as a pandas DataFrame](https://stackoverflow.com/questions/14365542/import-csv-file-as-a-pandas-dataframe) – not2qubit Nov 01 '20 at 01:01

2 Answers2

1

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)
idontknow
  • 438
  • 5
  • 16
  • Thank you very much. Your code indeed works! The output is like [('0', '1'), ('0', '6'), ('1', '7'),...]. If I want to remove '' on numbers, how should I amend? – qazwsx Nov 01 '20 at 01:17
  • The quotation marks in Python are indicators that they are strings. You can convert them to integers by using int(variable). Replace the line with this: `dataset.append((int(row[1]), int(row[2]),))` – idontknow Nov 01 '20 at 01:30
  • You can also use the bulletin map(), which makes every item in an iterable a specific type. `dataset.append(tuple(map(int, (row[1], row[2],))))`. – idontknow Nov 01 '20 at 01:32
  • Cool! Very helpful! Have a nice day :) – qazwsx Nov 01 '20 at 01:34
  • If this helped you, you can mark this Stack Overfllow answer as accepted. It will help anyone else who comes by to see which answer worked for you, and it will also gain me some rep! – idontknow Nov 01 '20 at 01:34
0

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')]
not2qubit
  • 14,531
  • 8
  • 95
  • 135