我需要您的帮助以使用python解决此问题。我有一个名为good_files.txt的.txt文件,其中每行都是一个通向新文件的路径(good_files.txt和包含这些文件的目录在同一目录中)。在每个文件中,我必须将三列数据汇总在一起以使曲线拟合
例如文件的结构是这样的。
1000.415915 225.484744 -2.012516
2.000945
215 0 0
219 0 0
222 4 0
224 70 70
226 696 696
229 999 1000
233 1001 1000
238 1001 1000
因此,我必须消除保留前三列的前两行,然后消除仅保留前两列的第三列。
第一列是我的x
坐标,第二列是我的y
坐标。
使用我的x and y
和错误函数erf
,我必须执行curve fitting
。
目前,我编写的唯一代码是用于读取good_files.txt
def ReadFromFile (fileName):
sourceFile= open (fileName, 'r')
text=[]
for adress in sourceFile.readlines ():
if '\n' in adress: text.append (adress [:-1])
else: text.append (adress)
return text
sourceFile.close()
def WriteToFile (text):
resultFile = open ('result.txt','w')
for data in text:
resultFile.write (data + '\n')
resultFile.close()
adresses = ReadFromFile ('good_files.txt')
for adress in adresses:
text = ReadFromFile (adress)
WriteToFile(text)
抱歉,目前我是一个菜鸟。感谢您的帮助,伙计们<3
python大神给出的解决方案
您可以使用Pandas来帮助阅读和合并csv文件。
假设您的ReadFromFile函数为您提供了一个不错的文件名列表,您可以执行以下操作:
import pandas as pd
def ReadFromFile (fileName):
sourceFile= open (fileName, 'r')
text=[]
for adress in sourceFile.readlines ():
if '\n' in adress: text.append (adress [:-1])
else: text.append (adress)
return text
sourceFile.close()
adresses = ReadFromFile('claro_good_files.txt')
df_list = [] # create empty list that will hold your dataframes from the seperate csv's
for adress in adresses:
# load each file, skipping the first two rows of data, splitting columns by whitespace, and setting the column names
df_temp = pd.read_csv(adress, skiprows=2, delim_whitespace=True, header=None, names=['x', 'y', 'remove'])
# add the 'x' and 'y' columns to the list of data frames to combine (exclude 'remove' column)
df_list.append(df_temp[['x', 'y']])
df = pd.concat(df_list) # Combine all the DataFrame's into one big one
这应该为您提供一个带有x
和y
列的数据框。
由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…
Python:同时在for循环中添加到列表列表 - python我想用for循环外的0索引值创建一个新列表,然后使用for循环添加到相同的列表。我的玩具示例是:import random data = ['t1', 't2', 't3'] masterlist = [['col1', 'animal1', 'an…
Python sqlite3数据库已锁定 - python我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…
查找字符串中的行数 - python我正在创建一个python电影播放器/制作器,我想在多行字符串中找到行数。我想知道是否有任何内置函数或可以编写代码的函数来做到这一点:x = """ line1 line2 """ getLines(x) python大神给出的解决方案 如果换行符是'\n',则nlines …