我试图从csvfile中编写两个txt文件(Test_8.txt和Test_9.txt)。 从COL4行我得到单引号和双引号以及“[”
我怎样才能摆脱它们
NR;COL1;COL2;COL3;COL4;COL5;COL6;COL7;REMARK
Test_9;96;0;4.26;4;5.25;-0.01;1;Test_9 tested, python
Test_9;96;0;4.26;4;11.75;2.35;1;Test_9 tested, python
Test_9;96;0;4.26;4;-3;-3;0.9;Test_9 tested, python
Test_8;95;0;4.25;3;4.75;-0.11;1;Test_8 tested, python
Test_8;95;0;4.25;3;-3;-3;0.9;Test_8 tested, python
Test_8;95;0;4.25;3;16.5;4.26;1;Test_8 tested, python
Test_8;95;0;4.25;3;12.751;2.861;1;Test_8 tested, python
TYPE 1.0
NR Test_8
COL1 95
COL2 0
COL3 4.250
COL4 3
-3.000 -3.000 0.900
4.750 -0.110 1.000
12.751 2.861 1.000
16.500 4.260 1.000
REMARK
Test_8 tested
with python
import os
import pandas as pd
pd.options.mode.chained_assignment = None
df=pd.read_csv(r'C:\Users\Desktop\test_map\test\mycsv_v1.csv',sep=';',index_col='NR')
df['COL3'] = df['COL3'].map('{:,.3f}'.format)
df['COL5'] = df['COL5'].map('{:,.3f}'.format)
df['COL6'] = df['COL6'].map('{:,.3f}'.format)
df['COL7'] = df['COL7'].map('{:,.3f}'.format)
ans = [[x,pd.DataFrame(y)] for x, y in df.groupby(df.index, as_index=True)]
#print ans
for table in ans:
line1=table[1].iloc[0]
#print line1
line1['TYPE']=1.0
line1['NR']=table[0]
col567=table[1][['COL5','COL6','COL7']].sort_values(by=['COL5'], ascending=True)
print col567
for row in range(len(col567)):
#print row
line1[str(col567.values[row])[1:-1]] = None
line1['']=None
col8=table[1]['REMARK'].str.split(',')[0]
col8=table[1]['REMARK'].str.split(', ')[1]
line1['REMARK']=str(col8.values[0])
line1['REMARK']=str(col8.values[1])
line1=line1[['TYPE', 'NR','','COL1','', 'COL2','', 'COL3', 'COL4',
str(col567.values[0:]), '', 'REMARK\n', col8.values[0],col8.values[1]]]
line1.to_csv(table[0]+'.txt',sep='\t')
TYPE 1.0
NR Test_8
COL1 95
COL2 0
COL3 4.250
COL4 3
"[['-3.000' '-3.000' '0.900']
['12.751' '2.861' '1.000']
['16.500' '4.260' '1.000']
['4.750' '-0.110' '1.000']]"
"REMARK
"
Test_8 tested
python
如果希望文本不带
[]
和quota
,则不要使用str()
和defalt格式,而是创建自己的函数来格式化文本。您可以为此使用" ".join()
和for
-loop示例代码
结果:
顺便说一句:您需要转换
col567.values[0:]
并在
我试图运行你的代码,但它有很多错误,它从来没有工作
使用字符串格式化的示例代码
我使用
io.StringIO
只是为了用数据模拟文件,但您使用pd.read_csv
顺便说一句:我必须更改一些元素,因为要获得正确排序的数据,它们必须是整数/浮点值,而不是字符串
{:,.3f}
您正在打印numpy.array的numpy.array。 默认格式为列表列表
您可以使用列表理解和字符串
join()
添加自己的格式此外,如果要使用
to_csv()
打印,则需要禁用引号。见对this question的答复相关问题 更多 >
编程相关推荐