- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想逐行读取两个 .txt 文件。文件的数据分为五列
FILE_1:
843.19598 2396.10278 3579.13778 4210.15674 4209.37549
841.93976 2397.21948 3573.11963 4205.89209 4226.73926
842.01642 2397.72266 3573.06494 4202.88379 4226.93799
842.22083 2397.47974 3574.27515 4204.19043 4223.82088
842.42065 2397.20142 3575.47437 4205.52246 4220.64795
FILE_2:
3586.02124 2391.50342 837.45227 -837.29681 -2385.97513
3587.69238 2387.48218 836.60445 -840.75067 -2390.17529
3588.44531 2387.44556 836.00555 -840.79022 -2389.77612
3588.08203 2388.25439 836.26544 -840.17017 -2389.07544
3587.66553 2389.05566 836.53046 -839.53912 -2388.40405
文件的每一行都必须转换为一个元组。例如,对于两个文件的第一行,输出应为:
FILE_1/1stLine = (843.19598, 2396.10278, 3579.13778, 4210.15674, 4209.37549)
FILE_2/1stline = (3586.02124, 2391.50342, 837.45227, -837.29681, -2385.97513)
然后我需要将这两个文件的行合并到一个名为 aux 的新变量中,其中第一个元素是 FILE_1 中的一行,第二个元素是 FILE_2 中相同位置的行
aux = (FILE_1/1stLine, FILE_2/1stline) ----- aux 1stLine
aux = (FILE_1/2ndLine, FILE_2/2ndline) ----- aux 2ndLine
.
.
aux = (FILE_1/LastLine, FILE_2/Lastline) ----- aux 2ndLastLine
例如,获取两个文件的第一行,第一个 aux 必须是:
((843.19598, 2396.10278, 3579.13778, 4210.15674, 4209.37549), (3586.02124, 2391.50342, 837.45227, -837.29681, -2385.97513))
有什么想法吗?
f1 = open("FILE_1.txt", "r")
f2 = open("FILE_2.txt", "r")
for a in f1:
for b in f2:
x = tuple(a)
y = tuple(b)
aux = (x, y)
此代码的结果是:
('8', '4', '3', '.', '1', '9', '5', '9', '8', ' ', '2', '3', '9', '6', '.', '1', '0', '2', '7', '8', ' ', '3', '5', '7', '9', '.', '1', '3', '7', '7', '8', ' ', '4', '2', '1', '0', '.', '1', '5', '6', '7', '4', ' ', '4', '2', '0', '9', '.', '3', '7', '5', '4', '9', '\n')
('3', '5', '8', '6', '.', '0', '2', '1', '2', '4', ' ', '2', '3', '9', '1', '.', '5', '0', '3', '4', '2', ' ', '8', '3', '7', '.', '4', '5', '2', '2', '7', ' ', '-', '8', '3', '7', '.', '2', '9', '6', '8', '1', ' ', '-', '2', '3', '8', '5', '.', '9', '7', '5', '1', '3', '\n')
(('8', '4', '3', '.', '1', '9', '5', '9', '8', ' ', '2', '3', '9', '6', '.', '1', '0', '2', '7', '8', ' ', '3', '5', '7', '9', '.', '1', '3', '7', '7', '8', ' ', '4', '2', '1', '0', '.', '1', '5', '6', '7', '4', ' ', '4', '2', '0', '9', '.', '3', '7', '5', '4', '9', '\n'), ('3', '5', '8', '6', '.', '0', '2', '1', '2', '4', ' ', '2', '3', '9', '1', '.', '5', '0', '3', '4', '2', ' ', '8', '3', '7', '.', '4', '5', '2', '2', '7', ' ', '-', '8', '3', '7', '.', '2', '9', '6', '8', '1', ' ', '-', '2', '3', '8', '5', '.', '9', '7', '5', '1', '3', '\n'))
非常感谢!
我不需要像“843.19598”这样获取 f1/f2 的每个元素,而是需要像 843.19598 这样不带引号的元素。
让我展示这些数据作为输入的代码(有一组点作为示例)
问题是我必须从这些文件中读取 x 和 y,并且对于每个集合,我需要拟合一个椭圆。
import ellipses as el
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
x = (5727.53135, 7147.62235, 10330.93573, 8711.17228, 7630.40262,
4777.24983, 4828.27655, 9449.94416, 5203.81323, 6299.44811,
6494.21906)
y = (67157.77567 , 66568.50068 , 55922.56257 , 54887.47348 ,
65150.14064 , 66529.91705 , 65934.25548 , 55351.57612 ,
63123.5103 , 67181.141725, 56321.36025)
data = (x, y)
lsqe = el.LSqEllipse()
lsqe.fit(data)
center, width, height, phi = lsqe.parameters()
print (center, width, height, phi)
plt.close('all')
fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111)
ax.axis('equal')
ax.plot(data[0], data[1], 'ro', label='test data', zorder=1)
ellipse = Ellipse(xy=center, width=2*width, height=2*height, angle=np.rad2deg(phi),
edgecolor='b', fc='None', lw=2, label='Fit', zorder = 2)
ax.add_patch(ellipse)
plt.legend()
plt.show()
最佳答案
FILE 1 (saved as f1.csv and f1.xls)
843.19598 2396.10278 3579.13778 4210.15674 4209.37549
841.93976 2397.21948 3573.11963 4205.89209 4226.73926
842.01642 2397.72266 3573.06494 4202.88379 4226.93799
842.22083 2397.47974 3574.27515 4204.19043 4223.82088
842.42065 2397.20142 3575.47437 4205.52246 4220.64795
FILE 2 (saved as f2.csv and f2.xls)
3586.02124 2391.50342 837.45227 -837.29681 -2385.97513
3587.69238 2387.48218 836.60445 -840.75067 -2390.17529
3588.44531 2387.44556 836.00555 -840.79022 -2389.77612
3588.08203 2388.25439 836.26544 -840.17017 -2389.07544
3587.66553 2389.05566 836.53046 -839.53912 -2388.40405
import csv
# Files to read
files = ['f1.csv', 'f2.csv']
tup_files = ()
aux = ()
# Read each file and concatenate to tup_files
for file in files:
with open(file) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=' ')
tmp_rows = ()
for row in csv_reader:
tmp_rows += (tuple(row), )
tup_files += (tmp_rows, )
for row_f1, row_f2 in zip(tup_files[0], tup_files[1]):
aux += (row_f1, row_f2)
print(f'printing f1\n{tup_files[0]}\n')
print(f'printing f2\n{tup_files[1]}\n')
print(f'printing aux\n{aux}')
import pandas as pd
# Files to read
files = ['f1.xls', 'f2.xls']
tup_files = ()
aux = ()
# Read each file and concatenate to tup_files
for file in files:
data = pd.read_excel(file, header=None)
tup_files += (tuple(data.itertuples(index=False, name=None)), )
for row_f1, row_f2 in zip(tup_files[0], tup_files[1]):
aux += (row_f1, row_f2)
print(f'printing f1\n{tup_files[0]}\n')
print(f'printing f2\n{tup_files[1]}\n')
print(f'printing aux\n{aux}')
printing f1
(('843.19598', '2396.10278', '3579.13778', '4210.15674', '4209.37549'),
('841.93976', '2397.21948', '3573.11963', '4205.89209', '4226.73926'),
('842.01642', '2397.72266', '3573.06494', '4202.88379', '4226.93799'),
('842.22083', '2397.47974', '3574.27515', '4204.19043', '4223.82088'),
('842.42065', '2397.20142', '3575.47437', '4205.52246', '4220.64795'))
printing f2
(('3586.02124', '2391.50342', '837.45227', '-837.29681', '-2385.97513'),
('3587.69238', '2387.48218', '836.60445', '-840.75067', '-2390.17529'),
('3588.44531', '2387.44556', '836.00555', '-840.79022', '-2389.77612'),
('3588.08203', '2388.25439', '836.26544', '-840.17017', '-2389.07544'),
('3587.66553', '2389.05566', '836.53046', '-839.53912', '-2388.40405'))
printing aux
(('843.19598', '2396.10278', '3579.13778', '4210.15674', '4209.37549'),
('3586.02124', '2391.50342', '837.45227', '-837.29681', '-2385.97513'),
('841.93976', '2397.21948', '3573.11963', '4205.89209', '4226.73926'),
('3587.69238', '2387.48218', '836.60445', '-840.75067', '-2390.17529'),
('842.01642', '2397.72266', '3573.06494', '4202.88379', '4226.93799'),
('3588.44531', '2387.44556', '836.00555', '-840.79022', '-2389.77612'),
('842.22083', '2397.47974', '3574.27515', '4204.19043', '4223.82088'),
('3588.08203', '2388.25439', '836.26544', '-840.17017', '-2389.07544'),
('842.42065', '2397.20142', '3575.47437', '4205.52246', '4220.64795'),
('3587.66553', '2389.05566', '836.53046', '-839.53912', '-2388.40405'))
根据需要使用元组的结果。
关于python - 将 .txt 或 excel 文件的行读入元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58789843/
假设您有 2 个文件,如下所示。 file_1_october.csv file_2_november.csv 文件具有相同的列。所以我想在 R 中读取这两个文件,我可以使用 map 轻松完成。我还想
我有一个制表符分隔的文本文件: 0730000 John 1 01 225 000 000 当我将它读入 R 时 stud_stats data.table::f
似乎最直观的是 .rdata 文件可能是 R 加载的快速文件格式,但是在扫描一些堆栈帖子时,似乎更多的注意力集中在提高 .csv 或其他格式的加载时间上。有确定的答案吗? 最佳答案 不是一个明确的答案
我是 R 的新手,目前在读取 .csv 文件并将其转换为 data.frame 时遇到了很多麻烦7 列。这是我正在做的: gene_symbols_table head(gene_symbols_t
基本上我有一个格式如下所示的 csv: csv 有 11 列,前五列和后五列完全相同。我希望能够读取 csv 并将第一列和第五列(期间和支出)的所有实例存储在一个列表中,它们具有值,并对另一个列表中的
我对 Julia 比较陌生,正在寻找一种有效的方法来从文本文件中读取并将每个“列”存储在数组中(我有 2 列,但通用解决方案也很棒)。例如,我想要输入 1 2 3 4 5 6
基本上我有一个格式如下所示的 csv: csv 有 11 列,前五列和后五列完全相同。我希望能够读取 csv 并将第一列和第五列(期间和支出)的所有实例存储在一个列表中,它们具有值,并对另一个列表中的
我的程序分配了一个 32 位 int,随后尝试使用 read(2) 从套接字将 4 个字节读入 int 有时读取不完整并返回读取 2 个字节。有什么方法可以从中恢复吗?我想我必须在 int 的中途生成
我有大量的 CSV 文件。有些标题从第一行开始,其他标题从第 3 行开始,其他的从第 7 行开始,依此类推。 标题看起来都一样,它们只是从不同文件的不同行开始。有没有办法有条件地 read.csv 文
我写了一个小程序来从 csv 文件中读取数据: using System; using System.Collections.Generic; using System.Linq; using Sys
我需要读入一个包含 10,000 个整数的列表,并将它们按升序放置在一个 vector 中。请注意,我不是在然后阅读排序,而是在同时阅读时排序。 我这样做是为了学习。我意识到阅读时排序是 O(n^2)
我有一个问题。不幸的是,我没有找到任何答案。如何将参数传递给脚本,这是另一个命令的结果。例如: ls | ./myscript.sh 我想将 ls 的结果传递给 myscript。如果我执行上面的命
我在读取扩展 ASCII 字符并将其转换为十进制值时遇到问题。我试过这样做: unsigned char temp; while(temp = cin.get != EOF) { cout << (i
我已经通过以下命令加载了文本文件。我想从 contents 中删除由 \n 分隔的第一行标题行。怎么做? txtfile = open(filepath, "rt") contents = txtfi
希望一切顺利...我正在将数据集输入到 sklearn 算法中进行分类,但找不到任何简单的数据集来开始,所以我自己制作了数据集。但有一个问题... import numpy as np import
我有一个 .csv 文件,它有 3 行和 5 列,值为 0、1、2、3、50 或 100。我将它从 Excel 工作表保存到 .csv 文件。我正在尝试使用 C++ 读取 .csv 文件,并根据最后三
我有一个 HTML 文件,它将作为我要发送的电子邮件的模板。 html 中有一些字段是可变的。我想知道是否有一种可靠的方法可以用变量替换 HTML 文件中的占位符。我知道我可以 string.Repl
我从未使用过 JSON 文件,但我有实现 JSON 文件的任务,我需要将其转换为 IEnumerable。当我尝试对 JSON 对象进行反序列化时,我得到一个异常,上面写着: An unhandled
我正在尝试阅读 IFormFile从这样的 HTTP POST 请求中收到: public async Task UploadDocument([FromForm]DataWrapper data)
我有一个包含大量多行文本 block 的文件。我想将该文件读入一个字符向量列表——每个 block 一个。我对 scan()、read.table() 等函数的文档的阅读似乎表明一行的结尾将结束向量。
我是一名优秀的程序员,十分优秀!