I want to split this text file into 3 (called x
, y
, and e
) lists using Python and I can't seem to do it.
This is what the text file (called data) looks like:
x y e
-2 2.1 0.170358869161
0 2.4 0.170202773308
2 2.5 -0.138557648063
4 3.5 0.187965696415
6 4.2 -0.473073365465
This is the code I have so far that doesn't work:
x=[]
y=[]
e=[]
try:
data = open('data.txt', 'rt')
except:
sys.exit('cannot find file')
with data:
try:
x.append(data[:1])
y.append(data[2:3])
e.append(data[4:5])
except:
sys.exit('cannot create lists')
Do this:
with data as f: # f is the file object
for line in f: # you can iterate over a file object line-by-line
xi, yi, ei = line.split()
x.append(xi)
y.append(yi)
e.append(ei.strip()) # ei will have a \n on the end
You can coerce them to ints or floats when appending them, if your assumptions about their shape are correct.
If you have pandas, I recommend read_csv
:
>>> import pandas as pd
>>> pd.read_csv('data.txt', delim_whitespace=True)
x y e
0 -2 2.1 0.170359
1 0 2.4 0.170203
2 2 2.5 -0.138558
3 4 3.5 0.187966
4 6 4.2 -0.473073
You can use the csv lib:
import csv
x, y, e = [], [], []
with open("in.csv") as f:
next(f)
for a, b, c in csv.reader(f, delimiter=" ", skipinitialspace=1):
x.append(float(a))
y.append(float(b))
e.append(float(c))
Output:
[-2.0, 0.0, 2.0, 4.0, 6.0]
[2.1, 2.4, 2.5, 3.5, 4.2]
[0.170358869161, 0.170202773308, -0.138557648063, 0.187965696415, -0.473073365465]
Or using with a defaultdict to group the elements:
import csv
from collections import defaultdict
with open("in.csv") as f:
d = defaultdict(list)
for dct in csv.DictReader(f, delimiter=" ", skipinitialspace=1):
for k, v in dct.items():
d[k].append(float(v))
from pprint import pprint as pp
pp(dict(d))
Output:
{'e': [0.170358869161,
0.170202773308,
-0.138557648063,
0.187965696415,
-0.473073365465],
'x': [-2.0, 0.0, 2.0, 4.0, 6.0],
'y': [2.1, 2.4, 2.5, 3.5, 4.2]}
I see that the replies above have answers that used hard-coded variables. However I found a way to create lists that can hold infinite lines.
def getFrom(index: int, data: list):
returnList = []
for item in data:
if len(item) >= index:
returnList.append(item[index])
else:
returnList.append("")
return returnList
In this function, data is the list that you want to search, can be nested, while index is the index you wanted to search. This will return a list contains that was in the index on the vertical space.
You can also try
data = ...
endList = [getFrom(index, data) for index in range(len(data))]
and endList if the result of the array split vertically.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.