发布于 2021-08-18 14:30:07
如果将"Drilling good ground all shift“移动到最左侧的列,使文件看起来如下所示:
Drilling good ground all shift
2 x Gyro Surveys
Mixing muds to condition the hole
Driller travelled home for shift change at end of shift
Equipment onsite=然后我相信您需要结合使用','.join(array)和split(',,', ',')来去掉空行,如下所示:
>>> import numpy as np
>>> data = np.loadtxt('Test.csv')
>>> data
array(['Drilling good ground all shift', '2 x Gyro Surveys',
'Mixing muds to condition the hole',
'Driller travelled home for shift change at end of shift', '',
'Equipment onsite='], dtype='<U55')
>>> ','.join(data).replace(',,', ',')
'Drilling good ground all shift,2 x Gyro Surveys,Mixing muds to condition the hole,Driller travelled home for shift change at end of shift,Equipment onsite='如果您不想手动更改Test.csv,可以使用Pandas执行此操作,将其转换为数组,然后按照上面的步骤进行操作:
>>> import pandas as pd
>>> all_dfs_1 = pd.read_csv(r"Test.csv", header=None)
>>> all_dfs_1
0 1 2 3 4 ... 7 8 9 10 11
0 Comments & Equip. Transfers Drilling good ground all shift NaN NaN NaN ... NaN NaN NaN NaN NaN
1 2 x Gyro Surveys NaN NaN NaN NaN ... NaN NaN NaN NaN NaN
2 Mixing muds to condition the hole NaN NaN NaN NaN ... NaN NaN NaN NaN NaN
3 Driller travelled home for shift change at end... NaN NaN NaN NaN ... NaN NaN NaN NaN NaN
4 NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN
5 Equipment onsite= NaN NaN NaN NaN ... NaN NaN NaN NaN NaN
[6 rows x 12 columns]
>>> all_dfs_1.iloc[0, 0] = all_dfs_1.iloc[0, 1]
>>> all_dfs_1[0]
0 Drilling good ground all shift
1 2 x Gyro Surveys
2 Mixing muds to condition the hole
3 Driller travelled home for shift change at end...
4 NaN
5 Equipment onsite=
Name: 0, dtype: object
>>> data = all_dfs_1[0].values
>>> data
array(['Drilling good ground all shift', '2 x Gyro Surveys',
'Mixing muds to condition the hole',
'Driller travelled home for shift change at end of shift', '',
'Equipment onsite='], dtype='<U55')
>>> ','.join(data).replace(',,', ',')
'Drilling good ground all shift,2 x Gyro Surveys,Mixing muds to condition the hole,Driller travelled home for shift change at end of shift,Equipment onsite='发布于 2021-08-18 14:31:06
不确定这是否是您想要的,它返回一个序列,该序列是数据框中的单个行,所有这些值连接在一起
import pandas as pd
import io
#"reads in" the csv file from a string so it can be tested without the file
all_dfs_1 = pd.read_csv(
io.StringIO(
"""
Comments & Equip. Transfers
2 x Gyro Surveys
Mixing muds to condition the hole
Driller travelled home for shift change at end of shift
Equipment onsite=
"""
),
header=None
)
#you'll want to do this instead since you have the file
#all_dfs_1 = pd.read_csv("Test.csv",header=None)
single_row = all_dfs_1.apply(lambda v: ','.join(v))
print(single_row)输出为
0 Comments & Equip. Transfers,2 x Gyro Surveys,M...
dtype: object如果你只是想要一个字符串,你也可以这样做:
','.join(all_dfs_1[0].values)https://stackoverflow.com/questions/68834138
复制相似问题