[英]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?
[英]I want to extract specific lines which contains x & y data from a TXT file and sort them by y data
这是我的 TXT 文件的屏幕截图,我已经确定了我应该提取它的部分
我想从 TXT 文件中提取包含销售数据(包括商品及其销售额)的特定行,并按销售额对它们进行排序。 结果应该是这样的,在不显示数量的情况下,根据它们的排序索引,在单独的行中显示每个项目及其索引,而不显示它们的数量:1- Citem 2- eitem 3- Ditem 4- Xitem 5- aitem 6- bitem 7- Yitem I使用此代码:我遇到错误
with open ('myfile', 'r') as myfile:
for myline in myfile:
if "sold" in myline:
item, amount = myline.split('(')
for index, item in enumerate((amount)):
print(index, item.rstrip("\n"))
[this the result (whole code)][1]
``` when I just extract the items without indexing and sorting them by amount its ok with the code below: but its not the answer that I want
with open ('myfile.txt', 'rt') as myfile:
for myline in myfile:
if "sold" in myline:
Item, Amount = myline.split('(')
print(Item.rstrip("\n"))
[Just extracting the Items without sorting them by amount][2]
[1]: https://i.stack.imgur.com/4gsUR.png
[2]: https://i.stack.imgur.com/nD2ha.png
如果文本文件格式与显示的完全一样,那么您可以这样做:
items = []
m = {'thousand': 1_000, 'million': 1_000_000}
with open('myfile.txt') as data:
for line in data:
if 'sold)' in line:
item, *e = line.split()
n = float(e[-3][1:]) * m.get(e[-2], 1)
items.append((n, item))
print(sorted(items))
Output:
[(13000.0, 'Yitem'), (120000.0, 'bitem'), (191000.0, 'aitem'), (2000000.0, 'Xitem'), (7200000.0, 'Ditem'), (32000000.0, 'eitem'), (96300000.0, 'Citem')]
这是执行此操作的一种方法:
with open ('myfile', 'r') as myfile:
data = myfile.readlines()
# match "thousand" and "million" to a number
scalekey = {"thousand": 1000, "million": 1000000}
items = [] # store item names
saleamount = [] # store amount of sales
# loop through the data
for line in data:
if "sold" in line:
itemline = line.strip().replace("sold", "") # remove sold from the line
items.append(itemline.split()[0]) # item name is the first value
# get the number of sold items from between the brackets
nsold = itemline[itemline.index("(") + 1: itemline.index(")")]
# convert the number into an integer
intnum = int(nsold.strip().split()[0])
# get the scale factor (i.e., thousand or million)
scale = scalekey[nsold.strip().split()[1]]
saleamount.append(intnum * scale)
# perform sorting
sortedlist = sorted(zip(saleamount, items))
print(sortedlist)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.