You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何替换CSV文件中的逗号为点号,让Python读取实验室测量数据

Solution to Fix CSV Decimal Separator for Python Plotting

Got it, let's get this CSV formatting sorted so you can create those clean Python plots for your lab measurements. The core issue is swapping commas to dots for decimal separators, and we can handle this directly in Python—no need to rely on Excel first.

Since you're working toward plotting, Pandas is the most efficient tool here—it’ll parse the CSV correctly and play seamlessly with plotting libraries like Matplotlib.

The trick is using two key parameters in pd.read_csv(): sep=';' to handle semicolon-delimited data, and decimal=',' to tell Pandas that commas are the decimal separators. This automatically converts all values (including scientific notation like 6,21E-09) to proper floats:

import pandas as pd
import matplotlib.pyplot as plt

# Load the CSV with correct separator settings
df = pd.read_csv('your_lab_data.csv', sep=';', decimal=',')

# Verify the data types are now floats
print(df.dtypes)

# Create your plot (customize labels/title to match your experiment)
plt.plot(df.iloc[:, 0], df.iloc[:, 1])
plt.xlabel('Input Values')
plt.ylabel('Measurement Readings')
plt.title('Lab Measurement Results')
plt.grid(True)
plt.show()

Method 2: Pure Python (No External Libraries)

If you’d rather skip Pandas, you can process the file manually line by line, replacing commas with dots and parsing values directly:

import matplotlib.pyplot as plt

x_data = []
y_data = []

# Open and process the raw data file
with open('your_lab_data.csv', 'r') as file:
    # Your sample data is all in one line—adjust if your file uses multiple lines
    raw_line = file.read().strip()
    # Split into individual data points
    data_points = raw_line.split()
    for point in data_points:
        x_str, y_str = point.split(';')
        # Swap commas to dots and convert to float
        x_val = float(x_str.replace(',', '.'))
        y_val = float(y_str.replace(',', '.'))
        x_data.append(x_val)
        y_data.append(y_val)

# Plot the cleaned data
plt.plot(x_data, y_data)
plt.xlabel('Input Values')
plt.ylabel('Measurement Readings')
plt.title('Lab Measurement Results')
plt.show()

Either approach will make sure Python recognizes all your numerical values as floats, so you can focus on refining your plots instead of fixing data formatting.

内容的提问来源于stack exchange,提问作者user12212883

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:52:33