我正在使用以下代码,使用python和pandas从csv读取特定行。 但是当我想打印公共数据,文本行时,我陷入了困境。 我要打印包含订单代码为和00157B的行。 PFA我正在使用的场景和附加代码的屏幕截图。
rows = pd.read_csv('SampData.csv', skiprows=[1,3])
print(rows.head())
答案 0 :(得分:0)
可以使用熊猫来做到这一点,但这太过分了。我只建议使用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]
答案 1 :(得分:0)
您可以尝试其中之一-
如果您想在OrderCode
列中进行整个术语的比较(例如00157B):
filtered = rows[rows['OrderCode'] == '00157B'].reset_index(drop=True)
filtered.to_csv('output.csv', index=False)
如果要在OrderCode
列中进行部分比较(例如OrderCodes
与< Test 1 >
):
filtered = rows[rows['OrderCode'].str.contains('< Test 1 >')].reset_index(drop=True)
filtered.to_csv('output.csv', index=False)