0
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 の2列目のコラムの長さ10以上を削除して、長さを10以下にする方法

プログラム

csv_modify.py
#! /usr/bin/python
#
#	csv_modify.py
#
#						Jul/13/2024
# ------------------------------------------------------------------
import pandas as pd
import csv
import sys

# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")

csv_file_in = sys.argv[1]
csv_file_out = sys.argv[2]
ll_limit = 10
df = pd.read_csv(csv_file_in,dtype=str)
print(df.shape)
column_index = 1
max_length = df.iloc[:,column_index].apply(len).max()
print("max_length =",max_length)
#
df.iloc[:, column_index] = df.iloc[:, column_index].str.slice(stop=ll_limit)

df.to_csv(csv_file_out,index=False,quoting=csv.QUOTE_ALL)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

実行

スクリプト

./csv_modify.py in01.csv out01.csv

入力 csv ファイル

in01.csv
"市区町村コード","宛名番号","抑止事由"
"131016","a1234","01"
"131016","a12345678912","01"
"131016","a123456789123","01"
"131016","a12345678912345","01"
"131016","a12345678912567","01"
"131016","a123456789","01"
"131016","a123","01"
"131016","a12345","01"

出力 csv ファイル

out01.csv
"市区町村コード","宛名番号","抑止事由"
"131016","a1234","01"
"131016","a123456789","01"
"131016","a123456789","01"
"131016","a123456789","01"
"131016","a123456789","01"
"131016","a123456789","01"
"131016","a123","01"
"131016","a12345","01"
0
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
0
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?