我有一个需要操作的.psa文件,但据我所知,该文件需要在.txt或.csv中才能使用PANAS进行操作.为了解决这个问题,我正在读取原始文件并将内容写入另一个.txt文件,我的代码将应用于该文件.
The original .psa file has text data in it, all separated by commas. I am trying to organize this data into columns and only pulling the data I need. Each line has 30+ values separated by commas, but I only need the 3rd value to put into a column.
I will have a zip folder which needs this code to run through and do the same thing to each file within the folder. Each file will have a different store number in the title.
例如:
文件名:1 Area 2 - store 15 group.psa
prod,123,456,abc,def, etc...
pla,124,uhj,jop,etc.
prod,321,789,ghi,jkl, etc...
...
期望值:
我只想删除以prod
开头的行中的第三项,并将其放入一个.csv文件中.我还想将原始文件的标题保留在另一栏中(如果只包含店号,但不是必需的,那就太好了).
前男友.
nums | store # |
---|---|
456 | 15 |
789 | 15 |
以下是我到目前为止拥有的代码:
with open('1 Area 2 - store 15 group.psa','r') as firstfile, open('test.txt','a') as secondfile:
# read content from first file
for line in firstfile:
# append content to second file
secondfile.write(line)
file = pd.read_csv("test.txt", sep=',', usecols=[0,1,2], header=0, names=['col 1','col 2','col 3'])
file.to_csv("output.csv", index=False)
这段代码能够给出列作为输出,但是行结束时包括不以prod开头的行,并且我有3列而不只是num列(当我只以[usecols=2]开头时得到一个错误,所以数据仍然混乱,并且我不知道如何在第二列中获得原始文件的标题.