将 csv 作为坐标导入 python
Import csv as coordinates to python
我是 python 的新手,现在需要使用“with...open...as”将给定的 csv 文件导入 pycharm,这是 csv 文件
enter image description here
我想要的输出是 [(0,1),(0,6),(1,7),...]
请问有人可以建议怎么写吗?
试试这个:
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.
您可以在此处找到 csv 模块的文档](https://docs.python.org/3/library/csv.html)。
这是官方 built-in 模块。
如果您愿意,您也可以通过为文件.readlines()
中的每个 line
调用 line.split(',')
来自己解析 csv,就像这样。
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)
这确实是重复的,但答案很简单:
# 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')]
我是 python 的新手,现在需要使用“with...open...as”将给定的 csv 文件导入 pycharm,这是 csv 文件 enter image description here
我想要的输出是 [(0,1),(0,6),(1,7),...]
请问有人可以建议怎么写吗?
试试这个:
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.
您可以在此处找到 csv 模块的文档](https://docs.python.org/3/library/csv.html)。 这是官方 built-in 模块。
如果您愿意,您也可以通过为文件.readlines()
中的每个 line
调用 line.split(',')
来自己解析 csv,就像这样。
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)
这确实是重复的,但答案很简单:
# 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')]