count = 0
fname = input["Enter file name: "]
fh = open[fname]
for line in fh:
if line.startswith["X-DSPAM-Confidence:"] :
print[line]
count = count + 1
print[count]
Có một tệp có 27 dòng như X-DSPAM-Confidence: 0.xxxxx, tôi cần trích xuất giá trị số từ mỗi chúng để được sử dụng để tính toán.
Hỏi ngày 17 tháng 12 năm 2018 lúc 16:42Dec 17, 2018 at 16:42
4
Cố gắng sử dụng
count = 0
fname = input["Enter file name: "]
fh = open[fname]
for line in fh:
if line.startswith["X-DSPAM-Confidence:"] :
print[line]
value = line.split[':'][-1] # will split line into 'X-DSPAM-Confidence' and 'value'
# if you have ',' at the end of the line, simply do this:
value = value.strip[',']
value = float[value]
print[value]
count = count + 1
print[count]
1:Code:
count = 0
fname = input["Enter file name: "]
fh = open[fname]
for line in fh:
if line.startswith["X-DSPAM-Confidence:"] :
print[line]
value = line.split[':'][-1] # will split line into 'X-DSPAM-Confidence' and 'value'
# if you have ',' at the end of the line, simply do this:
value = value.strip[',']
value = float[value]
print[value]
count = count + 1
print[count]
Đã trả lời ngày 17 tháng 12 năm 2018 lúc 16:49Dec 17, 2018 at 16:49
trsvchntrsvchntrsvchn
6.9923 Huy hiệu vàng22 Huy hiệu bạc29 Huy hiệu đồng3 gold badges22 silver badges29 bronze badges
Miễn là định dạng chính xác như bạn đã mô tả, bạn có thể sử dụng mã bên dưới:
float[line.split[':'][1]]
Nếu có nhiều biến thể trong văn bản hơn những gì bạn mô tả, bạn có thể cần phải thử regex.
Đã trả lời ngày 17 tháng 12 năm 2018 lúc 16:51Dec 17, 2018 at 16:51
AllenallenAllen
2162 Huy hiệu bạc10 Huy hiệu đồng2 silver badges10 bronze badges
Bạn có thể sử dụng
count = 0
fname = input["Enter file name: "]
fh = open[fname]
for line in fh:
if line.startswith["X-DSPAM-Confidence:"] :
print[line]
value = line.split[':'][-1] # will split line into 'X-DSPAM-Confidence' and 'value'
# if you have ',' at the end of the line, simply do this:
value = value.strip[',']
value = float[value]
print[value]
count = count + 1
print[count]
2 để có được vị trí của count = 0
fname = input["Enter file name: "]
fh = open[fname]
for line in fh:
if line.startswith["X-DSPAM-Confidence:"] :
print[line]
value = line.split[':'][-1] # will split line into 'X-DSPAM-Confidence' and 'value'
# if you have ',' at the end of the line, simply do this:
value = value.strip[',']
value = float[value]
print[value]
count = count + 1
print[count]
3 và sau đó thực hiện một lát chuỗi để có được giá trị.count = 0
fname = input["Enter file name: "]
fh = open[fname]
for line in fh:
if line.startswith["X-DSPAM-Confidence:"] :
print[line]
value = line[line.rfind[':']:] # will take the last occurrence of : to slice the line
print[value]
count = count + 1
print[count]
Đã trả lời ngày 17 tháng 12 năm 2018 lúc 16:52Dec 17, 2018 at 16:52
ycxycxycx
3.1252 Huy hiệu vàng13 Huy hiệu bạc26 Huy hiệu đồng2 gold badges13 silver badges26 bronze badges
fname = input["Enter file name: "]
fh = open[fname]
count = 0
pos = 0
ans = None
total = 0
for line in fh:
if not line.startswith["X-DSPAM-Confidence:"] :
continue
else :
count = count + 1
pos = line.find[':']
ans = line[pos+1 : ]
total = total + float[ans]
avg = total/count
Đã trả lời ngày 26 tháng 6 năm 2020 lúc 19:21Jun 26, 2020 at 19:21
Taj99Taj99Taj99
11 Huy hiệu Đồng1 bronze badge
2
fname = input["Enter file name: "]
fh = open[fname]
val = 0
count = 0
for line in fh:
if line.startswith["X-DSPAM-Confidence:"] :
count = count + 1
val=val + float[line[line.find['0']:]]
elif not line.startswith["X-DSPAM-Confidence:"] :
continue
print["Average spam confidence:",val/count]
Đã trả lời ngày 3 tháng 7 năm 2020 lúc 11:05Jul 3, 2020 at 11:05
fname = input["Enter file name:"]
fh = open[fname]
count = 0
s=0
for line in fh:
if not line.startswith["X-DSPAM-Confidence:"]:
continue
count = count+1
pos = line.find['0']
floatingP = float[line[pos:]]
s += floatingP
print[s/count]
Đã trả lời ngày 23 tháng 10 năm 2021 lúc 10:34Oct 23, 2021 at 10:34
Hướng dẫn này cho thấy các ví dụ để trích xuất giá trị nổi từ một chuỗi trong Python.
Ví dụ: Trích xuất giá trị nổi từ một chuỗi trong Python
Giả sử bạn có một số chuỗi tương tự
count = 0
fname = input["Enter file name: "]
fh = open[fname]
for line in fh:
if line.startswith["X-DSPAM-Confidence:"] :
print[line]
value = line.split[':'][-1] # will split line into 'X-DSPAM-Confidence' and 'value'
# if you have ',' at the end of the line, simply do this:
value = value.strip[',']
value = float[value]
print[value]
count = count + 1
print[count]
4 và bạn muốn trích xuất chỉ số điểm nổi.user_input = "Current Level: 4.89 db" for token in user_input.split[]: try: print [float[token], "is a float"] except ValueError: print [token, "is something else"]
Output:
Current is something else Level: is something else 4.89 is a float db is something else
Ví dụ 2: Sử dụng thư viện Re
import re re.findall["\d+\.\d+", "Current Level: 47.5 db."]
Output:
count = 0
fname = input["Enter file name: "]
fh = open[fname]
for line in fh:
if line.startswith["X-DSPAM-Confidence:"] :
print[line]
value = line.split[':'][-1] # will split line into 'X-DSPAM-Confidence' and 'value'
# if you have ',' at the end of the line, simply do this:
value = value.strip[',']
value = float[value]
print[value]
count = count + 1
print[count]
0Tham khảo: & nbsp; trích xuất float từ một chuỗi trong python.
Xem thêm:
- Thêm hai danh sách trong Python