1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CSVファイルのファイル名と行番号の文字列を取得する

Posted at
test.py
import csv

EXTRACT_LINENO_BEFORE = 1
EXTRACT_LINENO_AFTER = 1

def get_code_line(src_lines, lineno):
    if 0 < lineno <= len(src_lines):
        start_line = max(0, lineno - (EXTRACT_LINENO_BEFORE+1))
        end_line = min(len(src_lines), lineno + EXTRACT_LINENO_AFTER)
        result_lines = src_lines[start_line:end_line]
        code_lines = ''
        for i in range(len(result_lines)):
            code_lines += str((lineno - EXTRACT_LINENO_BEFORE)+i) + " : " + result_lines[i]
        return code_lines
    else:
        return "-"

def qac_result_extract_sourcecode(csv_reader_filepath, csv_writer_filepath):
    try:
        with open(csv_writer_filepath, 'w', newline='') as writerfile:
            writer = csv.writer(writerfile)

            try:
                #with open(csv_reader_filepath, 'r', encoding='utf-8') as csvfile:
                with open(csv_reader_filepath, 'r') as readerfile:
                    reader = csv.reader(readerfile)
                    next(reader)  # 1行目スキップ

                    prev_src_filepath = ""
                    prev_src_lineno = 0
                    code_lines = ""
                    src_lines = []
                    for row in reader:
                        if len(row) >= 2:
                            src_filepath = row[0]
                            src_lineno = int(row[1])

                            if prev_src_filepath == src_filepath and prev_src_lineno == src_lineno:
                                #print(code_lines)
                                print("**")

                            elif prev_src_filepath == src_filepath:
                                code_lines = get_code_line(src_lines, src_lineno)

                            elif prev_src_filepath != src_filepath:
                                with open(src_filepath, 'r', encoding='utf-8') as file:
                                    src_lines = file.readlines()
                                    code_lines = get_code_line(src_lines, src_lineno)
                            
                            prev_src_filepath = src_filepath
                            prev_src_lineno = src_lineno

                            row.append(code_lines)
                            writer.writerow(row)
                            
                            print(f"ファイルパス: {src_filepath}, 行番号: {src_lineno}, 結果:{code_lines}")
                        else:
                            print("不完全なデータ行が検出されました。")
            except FileNotFoundError:
                print("CSVファイルが見つかりません。")
            except Exception as e:
                print(f"エラーが発生しました: {e}")

    except Exception as e:
        print(f"エラーが発生しました: {e}")

if __name__ == "__main__":
    csv_reader_filepath = "C:\\Users\\EijiA\\OneDrive\\デスクトップ\\tools\\test.csv"
    csv_writer_filepath = "C:\\Users\\EijiA\\OneDrive\\デスクトップ\\tools\\test2.csv"
    qac_result_extract_sourcecode(csv_reader_filepath, csv_writer_filepath)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?