Python学習メモ:txt、xlsxファイルをインポートして簡単な関数処理を実行する

1.txtファイル

        1.1 パス

file_path = "E:\Python Project\temp.txt" 
with open(file_path) as f:
    content1 = f.read()

ファイルをインポートする際、ファイルの絶対パスを直接指定すると、\P がエスケープ文字であるため、エラーが報告されます。

したがって、絶対パスの前に r を追加すると、引用符内の内容がエスケープ文字として認識されるのを防ぐことができます (引用符の前に f を追加すると、これが特殊な形式の文字列であることを示します。これには中括弧 {} と中括弧が含まれる場合があります)。その中の式。ここで、{} には式の値が入ります)

file_path = r"E:\Python Project\temp.txt" 
with open(file_path) as f:
    content1 = f.read()

または、相対パスを直接使用することもできます

file_path = "temp.txt" 
with open(file_path) as f:
    content1 = f.read()

相対パスはデフォルトで現在のフォルダーを識別します

        1.2 ファイルの読み込み

Python は次を使用してファイルを読み取ることができます

with open(file_path) as f:
    f.read()

 もちろん、open(file,'r') でファイルの読み取り属性を追加することもできます。

file_path = r"E:\Python Project\temp.txt"
f = open(file_path)

ファイルの内容は、それぞれ read、readline、readline を通じてファイル全体、1 行、または全行を読み取ってリストに入れることができます。以前に読み取られた内容は後で読み出されません。

 2.xlsxファイル

        2.1 ファイルの読み込み

パンダライブラリのexcel_read()を利用できます

import pandas
f = pandas.read_excel(r"E:\Python Project\1.xlsx")
print(f)

xlrd ライブラリの open_workbook() を直接使用することもできますが、xlrd ライブラリの最新バージョンでは xlsx のサポートが削除されています。

import xlrd
f = xlrd.open_workbook_xls(r"E:\Python Project\1.xlsx")

openpyxlのload_workbook()を使用することもできます。

import openpyxl as xl
f = xl.load_workbook('1.xlsx')

3. 演習: ファイルデータに対して簡単な関数処理を実行する

xlsx ファイルのデータの 1 列目と 2 列目のピアソン係数の計算

import openpyxl
import pandas
import math

f = pandas.read_excel(r"E:\Python Project\1.xlsx")
data1 = f.values
print(type(data1))
sum_ans0 = 0
sum_ans1 = 0
for i in data1:
    sum_ans0 += i[0]
    sum_ans1 += i[1]
ave_ans0 = sum_ans0 / len(data1)
ave_ans1 = sum_ans1 / len(data1)
sum_final0 = 0
sum_final1 = 0
sum_final2 = 0
for temp_i in data1:
    sum_final0 += (temp_i[0] - ave_ans0) * (temp_i[1] - ave_ans1)
    sum_final1 += math.pow((temp_i[0] - ave_ans0), 2)
    sum_final2 += math.pow((temp_i[1] - ave_ans1), 2)
pearson = sum_final0/(math.sqrt(sum_final1) * math.sqrt(sum_final2))
print(f"Pearson={pearson}")

f を取得した後、f.value を通じてデータのリストを取得し、リスト内のデータを走査してそれを解決します。

トラバーサルは、openpyxl のsheet.cell を通じて実現することもできます。

import openpyxl as xl
f = xl.load_workbook('1.xlsx')
sheet = f['Sheet1']
cell = sheet.cell(1, 1)
print(cell.value)
list_ans = []
for row in range(1, sheet.max_row + 1):
    list_ans.append([sheet.cell(row, 1).value, sheet.cell(row, 2).value])

        3.1 2次元リストの和の最適化

上記の演習には、各リストの最初の数値と 2 番目の数値をそれぞれ合計する必要がある 2 次元リストが含まれます。

         3.1.1 トラバース用

最も簡単な方法は、for を使用してリストを直接走査することです。

import openpyxl
import pandas
import math

f = pandas.read_excel(r"E:\Python Project\1.xlsx")
data1 = f.values
print(type(data1))
sum_ans0 = 0
sum_ans1 = 0
for i in data1:
    sum_ans0 += i[0]
    sum_ans1 += i[1]

        3.1.2合計

 合計を多次元リストに直接適用すると、それぞれ対応する合計が得られます。

import openpyxl
import pandas
import math

f = pandas.read_excel(r"E:\Python Project\1.xlsx")
data1 = f.values
print(type(data1))
sum_ans1 = sum(data1)

        3.2 バーチャートの描画

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
f = xl.load_workbook('1.xlsx')
sheet = f['Sheet1']
cell = sheet.cell(1, 1)
print(cell.value)
list_ans = []
for row in range(1, sheet.max_row + 1):
    list_ans.append([sheet.cell(row, 1).value, sheet.cell(row, 2).value])
plot_data = Reference(sheet, min_col=1, max_col=2, min_row=1, max_row=sheet.max_row)
chart = BarChart()
chart.add_data(plot_data)
sheet.add_chart(chart, 'c1')
f.save('1.xlsx')

おすすめ

転載: blog.csdn.net/YGZ11113/article/details/132788227