感谢您的阅读!我对此很陌生,但昨天我开始从网站提取数据(一个项目的名称及其相应的价格),并跳出如何使用Python 2.7为格式创建csv文件,格式为:price,item-name。现在我有一些数据要与它们进行比较,并获得给定项目名称的平均价格。我现在遇到的问题是每个网站上的项目名称(可能)略有不同。例如我的第一数据集给出了csv文件CSV文本清理和Python比较
4.0, Jungle Book
5.0, "Peter Pan"
4.0, Lady and the Tramp
第二给出
5.0, Disney's Jungle Book
6.0, Disney's Up
4.0, Disney's Peter Pan
第三给出
5.0, Up (DVD)
4.0, Peter pan (DVD)
6.0, "Lady and the Tramp" (DVD)
最后,我想像
平均输出文件4.5, Jungle Book
5.0, Lady and the Tramp
4.33, Peter Pan
5.5, Up
我的第一个问题是删除我的csv文件中的特殊字符,如“或某些词(如”迪士尼“,”(DVD)“)。我找到有关从csv文件中删除行和列的信息,但是我很难在这些元素内进行编辑。像这样的东西删除“(DVD)”之类的作品,但让我的CSV文件,甚至更大的混乱与更多的“和[人物..
import csv
import string
input_file = open('DesktopData.csv', 'r')
output_file = open('fixformat.csv', 'w')
data = csv.reader(input_file)
writer = csv.writer(output_file,quoting=csv.QUOTE_ALL)# dialect='excel')
specials = '(DVD)'
for line in data:
line = str(line)
new_line = str.replace(line,specials,'')
writer.writerow(new_line.split(','))
input_file.close()
output_file.close()
一旦将工作了,我想借此均价对于给定的称号。我心目中的东西,但缺乏Python语法真正弄明白
Read all titles and put in mainlist;
if title already exsists, ignore/dont make new row with title
Read all files and compare with mainlist;
if title is found, put corresponding price in new column behind title
在我的头上这已经到输出中
Jungle Book, 4.0, 5.0
Lady and the Tramp, 4.0, 6.0
Peter Pan, 5.0, 4.0, 4.0
Up, 6.0, 5.0
一旦我得到这个我敢肯定我可以做到到一般的csv文件。任何建议非常感谢!
你可能想看看导入熊猫,并与它玩。从查看pd.read_csv()方法开始。 – Aklys