我有一個帶有行和列變數的 .csv 檔案。行是B1-K1,列是A2-A7。實際資料是從B2到K7。我希望能夠將值放入串列中并呼叫它們,如果有辦法以 0,1,2,,3,4,5,0,1 的模式對資料(B2-K7)進行編號....那會很好。
import csv
file = open(r'C:/cpps/python project/prodata.csv')
csvreader = csv.reader(file)
header = next(csvreader)
print(header)
rows = []
for row in csvreader:
rows.append(row)
print(rows)
file.close()
uj5u.com熱心網友回復:
您可以使用pandas.
import pandas as pd
df = pd.read_csv('yourfilename.csv')
l = df.values.tolist() # this list will contain the whole csv file in a list.
uj5u.com熱心網友回復:
有很多方法可以給貓剝皮,但如果您只是閱讀 .csv(而不是 .xlsx),則不需要特定于 Excel 的庫:
# this code just (over)writes a .csv for demonstration purposes
from csv import writer
with open('block.csv', 'w', newline='') as f:
data = [[(r 1)*100 c 1 for c in range(15)] for r in range(9)]
cw = writer(f)
cw.writerows(data)
# this is how you can do it with pandas
import pandas as pd
df = pd.read_csv('block.csv', header=None)
# prints the entire dataframe
print(df)
# this selects the data you want (A2:A7), but it's still a dataframe
print(df.loc[1:6, 0:0])
# this selects the data, and the first columns from it, and then turns the values into a list
print(df.loc[1:6, 0:0][0].values.tolist())
# similarly, this selects B1:K1, turns the values into a list of lists and then selects the first
print(df.loc[0:0, 1:10].values.tolist()[0])
# you can also do it with the standard csv module
from csv import reader
# opening the file for reading
with open('block.csv', 'r') as f:
cr = reader(f)
# the reader will read the rows as a list of strings, we want numbers
data = [[int(x) for x in row] for row in list(cr)]
# prints the entire list of lists
print(data)
# this selects the first element from rows 1 through 6, so A2:A7
print([x[0] for x in data[1:7]])
# and from the first row, columns 2 through 11, so B1:K1
print(data[0][1:11])
# you don't need the csv module, although I would not recommend this, unless you must and you're
# certain your .csv doesn't have any problems
with open('block.csv', 'r') as f:
# just reading and splitting the lines and converting the values to integer
data = [[int(x) for x in line.strip().split(',')] for line in f]
# the rest of the code is the same as for the csv.reader
print(data)
print([x[0] for x in data[1:7]])
print(data[0][1:11])
作為獎勵,這里有一個函式,它允許您像在 Excel 中一樣使用一個范圍,以避免計數錯誤(就像我在第一次發布答案時所做的那樣):
import re
def col_to_index(col):
return sum((ord(c) - 64) * 26**i for i, c in enumerate(reversed(col))) - 1
def df_excel_range(df, excel_range):
import re
coords = [(col_to_index(cell[0]), int(cell[1])-1) for cell in [
re.match('([A-Z] )(\d )', cell).groups() for cell in excel_range.upper().split(':')]]
if len(coords) == 1:
coords = [*coords[0], *coords[0]]
elif len(coords) != 2:
raise SyntaxError(f'not a valid range {excel_range}')
return df.loc[coords[0][1]:coords[1][1], coords[0][0]:coords[1][0]]
您可以使用它從資料框中選擇一個范圍,如下所示:
print(df_excel_range(df, 'A2:A7')[0].values.tolist())
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/372786.html
