如何使用python脚本或Shell分割CSV文件中的coulmn文本?

2024-10-06 12:19:49 发布

您现在位置:Python中文网/ 问答频道 /正文

  1. 第1行\u 1368083 \u US \u PBPR \u STD
  2. 世界其他地区
  3. 第216行\u 60902413 \u US \u PBPR \u ENH
  4. 第227行第37758281行

最终输出只能是1368083列中的数字


Tags: 世界数字地区usstdenhpbpr
3条回答

使用str.split

s1 = "Row1_1368083_US_PBPR_STD"
s2 ="Row215_1368083_US_PBPR_ENH"

print(s1.split("_")[1])
print(s2.split("_")[1])

输出:

1368083
1368083

或者正则表达式。你知道吗

import re

s1 = "Row216_60902413_US_PBPR_ENH"
s2 ="Row227_37758281_US_PBPR_ENH"

print(re.findall(r"\d{6,}", s1)[0])
print(re.findall(r"\d{6,}", s2)[0])
awk -F_ '$2 ~/1368083/{print $2}' file
1368083
1368083

使用sed提取两个‘’

sed 's/^.*_\([0-9]*\)_.*/\1/'

或者使用awk提取第二个字段,该字段由‘’

awk -F'_' '{print $2}'

相关问题 更多 >