简体   繁体   English

如何读取 2 列 of.txt 文件并将其存储在 python 的 x 和 y 变量中

[英]How to read 2 columns of .txt file & store it in x & y variables in python

This is the data of.txt file.(2 columns)这是.txt文件的数据。(2列)

6.1101,17.592
5.5277,9.1302
8.5186,13.662
7.0032,11.854
5.8598,6.8233
8.3829,11.886
7.4764,4.3483
8.5781,12.0
6.4862,6.5987
5.0546,3.8166
5.7107,3.2522

you can use csv reader您可以使用csv reader

import csv
x = []
y = []
with open('file.txt') as f:
    reader = csv.reader(f)
    for row in reader:
        x.append(float(row[0]))
        y.append(float(row[1]))

Output : Output

>>x
[6.1101, 5.5277, 8.5186, 7.0032, 5.8598, 8.3829, 7.4764, 8.5781, 6.4862, 5.0546, 5.7107]
>>y
[17.592, 9.1302, 13.662, 11.854, 6.8233, 11.886, 4.3483, 12.0, 6.5987, 3.8166, 3.2522]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从.txt文件导入数据并在Python中定义x和y变量 - Import data from .txt file and define x and y variables in Python Python-使用Numpy,如何从.txt文件读取数据并将数据列分配给变量 - Python-Using Numpy, how to read the data from .txt file and assign the columns of data to variables 如何将.txt文件(CSV)中的列设置为变量? (蟒蛇) - How does one set columns in a .txt file (CSV) to variables? (PYTHON) 如何使用 tkinter (Python) 逐行读取变量.txt 文件 - How to read variables with tkinter (Python) using a line by line .txt file 如何在Python中以不同的字符串读取不同的.txt文件列? - How to read different .txt file columns in different strings in Python? 如何从CSV文件中提取数据列并将其定义为x和y变量,然后使用pylab在python中绘制它们? - How can I extract columns of data from a CSV file and define them as x and y variables, then plot them in python using pylab? 如何使用 opencv 将两个鼠标坐标 [(x0,y0),(x1,y1)] 导出到 python 中的 txt 文件 - How to export two mouse coordinates [(x0,y0),(x1,y1)] to a txt file in python, with opencv 如何从python中的轮廓线提取坐标并将其存储在新的x和y变量中? - How to extract coordinates from contour lines in python and store them in new x and y variables? 使用txt文件(Python)中的数据绘制日期和时间(x轴)与值(y轴)(仅用空格分隔的列) - Plot date and time (x axis) versus a value (y axis) using data from txt file (Python) (columns separated only by spaces) 如何使用CasperJS读取.txt文件并将信息存储为变量以供脚本使用? - How do I read a .txt file with CasperJS and store info as variables for the script to use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM