我有一个像这样的数字列表
-4.4987000e-01 -2.0049000e-01 -4.8729000e-01 -6.1085000e-02 -5.1024000e-02 -2.1653000e-02 3.0788000e-01 -5.7097000e-02 -1.5610000e-02 1.3241000e-01 3.6023000e-01 2.0951000e-01 -4.4486000e-04 4.5643000e-02 2.1099000e-01 5.5061000e-01 1.8639000e-01 5.8702000e-02 1.8000000e+01 -5.1000000e+01 2.9000000e+01 4.4000000e+01 -2.5000000e+01 2.0000000e+01 -7.8000000e+01 -4.0000000e+00 -5.7000000e+01 5.4868000e-03 -4.8718000e-03 3.1912000e-03 5.2027000e-05 3.4499000e-04 2.5141000e-03 -2.0454000e-04 -1.9931000e-03 -6.1825000e-04 2.0000000e+00 3.0000000e+00 1.0000000e+00 2.0000000e+00 1.0000000e+00 1.0000000e+00 3.0000000e+00 3.0000000e+00 1.0000000e+00 1.0000000e+00 1.0000000e+00 1.0000000e+00 3.5905000e-02 -1.0375000e+02 -1.1897000e+02 1.5385000e+02 -6.2466000e+01 2.0804000e+02 4.4696000e+00 -4.6665000e+01 1.4479000e+02 -2.5231000e+01 -2.0513000e+02 -4.0358000e+01 9.2609000e+01 8.8504000e+00 6.1909000e+01 6.9962000e+01 9.1949000e+01 -2.0233000e+02 -1.5710000e+01 -5.6219000e+01 -1.3258000e+01 2.3295000e+02 -2.5827000e+02 5.7025000e+01 6.0528000e+01 6.1473000e+01 5.5480000e+00 5.1505000e+01 2.9502000e+01 -2.0039000e+02 9.6260000e+00 4.4402000e+01 7.5328000e+01 3.3595000e+00 5.0048000e+01 5.8790000e+01 -2.7963000e+02 8.7446000e+01 2.9345000e+01 -6.6092000e+00 -1.6112000e+00 1.6497000e+02 -3.3687000e+01 1.7421000e+02 -1.4319000e+02 1.3931000e+02 -1.4961000e+02 1.6481000e+02 -1.7286000e+02 -1.8169000e+02 -1.7561000e+02 -1.7869000e+01 -1.3961000e+01 -9.2109000e+00 4.5912000e+00 2.3210000e+01 1.4623000e+02 -1.7808000e+02 1.5201000e+02 -1.2972000e+02 1.2648000e+02 -1.4733000e+02 1.6865000e+02 1.8033000e+02
-5.2843000e-01 -2.5912000e-01 -4.2584000e-01 -6.1339000e-02 -7.5853000e-02 -2.7442000e-02 3.0166000e-01 -6.4007000e-02 -4.2905000e-02 3.3057000e-01 2.3707000e-01 3.6533000e-01 6.5008000e-03 5.2863000e-02 2.2453000e-01 5.4726000e-01 1.7977000e-01 8.5435000e-02 2.5000000e+01 -2.3000000e+01 2.8000000e+01 4.6000000e+01 -5.4000000e+01 3.9000000e+01 -7.8000000e+01 -2.0000000e+00 -6.8000000e+01 7.6492000e-03 -4.4639000e-03 2.4742000e-03 7.4460000e-04 -6.5452000e-06 2.5198000e-03 -3.8057000e-04 -1.9782000e-03 -1.0207000e-03 1.0000000e+00 2.0000000e+00 1.0000000e+00 2.0000000e+00 1.0000000e+00 1.0000000e+00 2.0000000e+00 2.0000000e+00 1.0000000e+00 2.0000000e+00 1.0000000e+00 1.0000000e+00 3.3818000e-02 -7.9480000e+01 -7.4092000e+01 2.6918000e+02 -6.4591000e+00 -1.8279000e+02 7.5153000e+01 -4.4098000e+01 1.4419000e+02 -4.2769000e+00 -1.7227000e+02 -2.5832000e+01 -2.9895000e+01 6.0674000e+01 1.3823000e+02 5.6429000e+01 6.4801000e+01 -2.6334000e+02 3.4945000e+01 3.9544000e+01 4.4425000e+01 7.3094000e+01 -2.4618000e+02 5.5202000e+01 -4.3122000e+00 -3.5971000e+01 -4.0005000e+01 2.4211000e+02 -3.0704000e+01 -1.8313000e+02 -1.7990000e+00 -2.8792000e+01 2.9179000e+02 -1.7592000e+01 -7.4246000e+01 -1.7671000e+01 -2.2706000e+02 4.1538000e+01 2.5262000e+01 1.1633000e+02 2.7411000e+01 1.8463000e+00 -3.3862000e+01 1.7156000e+02 -1.4708000e+02 1.5506000e+02 -1.7217000e+02 1.8060000e+02 -1.7751000e+02 1.7789000e+02 -1.5722000e+02 1.2871000e+00 -4.9295000e+01 -2.8774000e+01 9.7736000e+00 -4.6825000e+00 1.0302000e+02 -1.8273000e+02 1.6897000e+02 -1.5129000e+02 1.2489000e+02 -1.1842000e+02 1.2541000e+02 2.0331000e+02
-4.9823000e-01 -2.6346000e-01 -4.0683000e-01 -5.0683000e-02 -6.6742000e-02 -2.4397000e-02 2.7500000e-01 -1.3061000e-01 -1.0567000e-01 2.8596000e-01 3.3375000e-01 2.6932000e-01 1.0920000e-02 5.4195000e-02 2.1799000e-01 5.4572000e-01 1.3984000e-01 1.1345000e-02 2.6000000e+01 -2.4000000e+01 2.9000000e+01 6.0000000e+01 -3.9000000e+01 4.2000000e+01 -7.8000000e+01 -3.0000000e+00 -6.7000000e+01 8.9250000e-03 -5.8038000e-03 2.7718000e-03 5.8406000e-04 -5.7379000e-05 2.4386000e-03 -6.5300000e-04 -2.4464000e-03 -8.5056000e-04 1.0000000e+00 3.0000000e+00 1.0000000e+00 1.0000000e+00 1.0000000e+00 1.0000000e+00 1.0000000e+00 2.0000000e+00 2.0000000e+00 1.0000000e+00 1.0000000e+00 1.0000000e+00 2.0957000e-02 -3.4940000e+01 1.9048000e+02 -1.2839000e+00 -2.6178000e+02 -3.8176000e+01 1.7091000e+02 -1.8192000e+01 -2.1046000e+01 -9.3653000e+01 -7.2957000e+01 -6.8327000e+01 -2.0539000e+00 2.2286000e+01 1.7142000e+02 3.9634000e+01 9.8349000e+01 -2.9960000e+02 6.2743000e+01 -1.7442000e+01 6.9406000e+01 9.1777000e+01 -2.5362000e+02 5.6482000e+01 1.0460000e+01 3.5242000e+01 8.3919000e+01 2.4787000e+00 1.6242000e+01 -2.1568000e+02 3.5917000e+01 -3.1472000e+01 2.4643000e+02 -6.7954000e+01 -4.5022000e+00 2.4380000e+01 -2.4936000e+02 5.4252000e+01 3.9900000e+01 6.1425000e+01 3.6881000e+01 1.0403000e+00 2.4462000e+01 1.7538000e+02 -1.4825000e+02 1.4900000e+02 -1.6719000e+02 1.7977000e+02 -1.8226000e+02 1.8268000e+02 -1.7823000e+02 -6.6030000e+01 2.1203000e+01 -5.1797000e+01 3.8823000e+01 -5.3340000e+01 1.6133000e+02 -1.8005000e+02 1.5152000e+02 -1.2785000e+02 1.1796000e+02 -1.2576000e+02 1.3976000e+02 1.9323000e+02
如您所见,它以2个空格开头。如果下一个数字有负号,它将有2个空格,如果没有,则有3个空格。我想要它,所以它以一个数字开头,然后用这样的逗号分隔:
-4.9823000e-01,-2.6346000e-01,-4.0683000e-01,-5.0683000e-02
我试过这个变种:
with open('file.txt', 'r') as data:
plaintext = data.read()
plaintext = plaintext.replace(' ', ',' )
plaintext = plaintext.replace(' ', ',' )
但无法找出解决这个问题的最佳方法。
编辑 -
这是数字的[来源] [1]
我编辑了数字列表。正如您所看到的,如果在1.8033000e + 02,2.0331000e + 02和1.9323000e + 02之后开始新线。我查看了数据,看看它是否有新行\n
,但它没有,这让我失望。我需要将每行作为一行放在CSV文件中。如何在添加到CSV文件时将其设为新行?
with open('file.txt', 'r') as data:
plaintext = data.read()
plaintext = plaintext.split();
plaintext = ','.join(plaintext)
with open('file.csv', 'w', newline='', encoding='utf-8') as csv_file:
csv_file.write(plaintext)
此代码会将所有内容放在CSV文件的一行中。
答案 0 :(得分:2)
您可以使用.split()按空格分割,然后使用.join将其重新组合在一起,并在数字之间插入逗号:
plaintext = data.read()
plaintext = ','.join(plaintext.split())
如果你想先用换行符分隔代码来为csv文件制作一个二维数组,那也没关系:
plaintext = data.read()
plaintext = [i.split() for i in plaintext.split('\n')]
将其写入csv:
import csv
with open(<yourfile>, 'w+') as f:
writer = csv.writer(f)
writer.writerows(plaintext)
答案 1 :(得分:2)
在空格上拆分,用逗号加入:
data = "-4.9823000e-01 -2.6346000e-01 -4.0683000e-01 5.0683000e-02 -6.6742000e-02"
parsed_data = ",".join(data.split())
# -4.9823000e-01,-2.6346000e-01,-4.0683000e-01,5.0683000e-02,-6.6742000e-02
更新:由于您正在从具有新行的文件中读取并且您希望保持行只是逐行读取文件,请执行相同的过程并将其写入新文件中:
with open("file.txt", "r") as f_in, open("file.csv", "w") as f_out: # open the files
for line in f_in: # read the input file line by line
f_out.write(",".join(data.split()) + "\n") # 'convert' and write to the output file
答案 2 :(得分:0)
一种方法是使用regex
用一个,
替换多个空格:
plaintext = data.read()
plaintext = re.sub(r"\s+", lambda m: ',', plaintext, flags=re.M)
示例强>:
s = "-4.9823000e-01 -2.6346000e-01 -4.0683000e-01 -5.0683000e-02"
print(re.sub(r"\s+", lambda m: ',', s, flags=re.M))
# -4.9823000e-01,-2.6346000e-01,-4.0683000e-01,-5.0683000e-02
答案 3 :(得分:0)
我很困惑为什么你的csv行不以'\ n'结尾。
无论如何,您可以使用readline()
newText=''
while True:
plainText=data.readline()
if plainText=='':
break
newText+=','.join(data.split())+'\n'