使用 Python 替换 csv 文件列中的模式
Replace a pattern in column of a csv file using Python
我在 python 中寻求有关编辑 CSV 的帮助。我有一个格式如下的文件
Account
InstanceId
InstanceName
InstanceType
Platform
Status
AvailabilityZone
PrivateIpAddress
publicIpAdress
Owner
12345678901
i-0abcdef1234567890
Test123
t3a.micro
windows
stopped
ap-southeast-2a
10.0.0.100
arn:aws:sts::12345678901:user/test-user
98765432109
i-0987654321abcdefg
test345
t2.nano
Linux
running
ap-southeast-2b
10.0.0.200
99.99.99.100
arn:aws:sts::98765432109:assumed-role/testrole/user@example.com
98765432109
i-abcdefghij01234899
test987
t2.nano
windows
running
ap-southeast-2b
10.0.0.201
我想就地编辑此文件,并使生成的文件格式如下
Account
InstanceId
InstanceName
InstanceType
Platform
Status
AvailabilityZone
PrivateIpAddress
publicIpAdress
Owner
12345678901
i-0abcdef1234567890
Test123
t3a.micro
windows
stopped
ap-southeast-2a
10.0.0.100
test-user
98765432109
i-0987654321abcdefg
test345
t2.nano
Linux
running
ap-southeast-2b
10.0.0.200
99.99.99.100
user@example.com
98765432109
i-abcdefghij01234899
test987
t2.nano
windows
running
ap-southeast-2b
10.0.0.201
我尝试了一些事情但没有成功,如果你能帮助我,我将不胜感激。
提前致谢。
使用:
df['Owner'] = df['Owner'].str.split('/').str[-1]
示范:
df = pd.DataFrame({'Owner': ['arn:aws:sts::12345678901:user/test-user']})
df['Owner'].str.split('/').str[-1]
输出:
我在 python 中寻求有关编辑 CSV 的帮助。我有一个格式如下的文件
Account | InstanceId | InstanceName | InstanceType | Platform | Status | AvailabilityZone | PrivateIpAddress | publicIpAdress | Owner |
---|---|---|---|---|---|---|---|---|---|
12345678901 | i-0abcdef1234567890 | Test123 | t3a.micro | windows | stopped | ap-southeast-2a | 10.0.0.100 | arn:aws:sts::12345678901:user/test-user | |
98765432109 | i-0987654321abcdefg | test345 | t2.nano | Linux | running | ap-southeast-2b | 10.0.0.200 | 99.99.99.100 | arn:aws:sts::98765432109:assumed-role/testrole/user@example.com |
98765432109 | i-abcdefghij01234899 | test987 | t2.nano | windows | running | ap-southeast-2b | 10.0.0.201 |
我想就地编辑此文件,并使生成的文件格式如下
Account | InstanceId | InstanceName | InstanceType | Platform | Status | AvailabilityZone | PrivateIpAddress | publicIpAdress | Owner |
---|---|---|---|---|---|---|---|---|---|
12345678901 | i-0abcdef1234567890 | Test123 | t3a.micro | windows | stopped | ap-southeast-2a | 10.0.0.100 | test-user | |
98765432109 | i-0987654321abcdefg | test345 | t2.nano | Linux | running | ap-southeast-2b | 10.0.0.200 | 99.99.99.100 | user@example.com |
98765432109 | i-abcdefghij01234899 | test987 | t2.nano | windows | running | ap-southeast-2b | 10.0.0.201 |
我尝试了一些事情但没有成功,如果你能帮助我,我将不胜感激。 提前致谢。
使用:
df['Owner'] = df['Owner'].str.split('/').str[-1]
示范:
df = pd.DataFrame({'Owner': ['arn:aws:sts::12345678901:user/test-user']})
df['Owner'].str.split('/').str[-1]
输出: