我正在使用以下代码,使用python和pandas从csv读取特定行。但是当我想打印公共数据,文本行时,我陷入了困境。我想将包含订单代码的行打印为和00157B。PFA我正在使用的场景和附加代码的屏幕截图。
rows = pd.read_csv('SampData.csv', skiprows=[1,3])
print(rows.head())
可以使用熊猫来做到这一点,但这太过分了。我只建议使用csv
模块,该模块非常易于使用,并且包含许多在线文档。这是一个玩具示例,几乎可以满足您的需求,我认为
data.csv文件:
Order Number, Item, quantity
76XY, Cheese, 3
88TG, Broccoli, 44
76XY, Cookies, 1000
98UU, Coke, 1
操作2个csv文件的简短文件:
import csv
input_file = 'data.csv'
output_file = 'found_orders.csv'
magic_order = '76XY'
with open(output_file, 'w') as target:
target_writer = csv.writer(target)
# open the source
with open(input_file, 'r') as source:
source_reader = csv.reader(source)
# now both are "open and ready"
# get the header from the first read of the source
header = source_reader.__next__()
# write to the modified file
target_writer.writerow(header)
# now use loop to loop through all rows and look for the order number
# if found, print it to console and write it out to new csv
# some extra print statements to see what is going on. Recall
# when csv reader reads a row, it reads it in as a list
for row in source_reader:
print('just read row: ', row)
order = row[0] # the position in the list of the order
if order == magic_order:
print('it matches the magic order')
target_writer.writerow(row)
else:
print('not a match')
print()
# if you use the 'with' command structure, it will close the files automatically
print('done')
输出(至控制台)。输出文件就是您期望看到的。:
just read row: ['76XY', ' Cheese', ' 3']
it matches the magic order
just read row: ['88TG', ' Broccoli', ' 44']
not a match
just read row: ['76XY', ' Cookies', ' 1000']
it matches the magic order
just read row: ['98UU', ' Coke', ' 1']
not a match
done
[Finished in 0.0s]